简体   繁体   English

jQuery和它的.getJSON不起作用,以及如何同步asynchronus .getJSON?

[英]jQuery and its .getJSON not working and also how to synchronise the asynchronus .getJSON?

so first of all, Yeah i know there are several answers out there, but none was able to solve my problem. 首先,是的,我知道有几个答案,但没有一个能够解决我的问题。 first i will show my code HTML: 首先,我将展示我的代码HTML:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shop</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <script src="https://use.fontawesome.com/a565674a9d.js"></script>


    <!--MINE-------------------------------->
    <script src="JS/jquery.js"></script>
    <script src="JS/catalog.js"></script>

</head>
<body>
<nav class="navbar navbar-light">
    <div>
    <a class="navbar-brand" style="text-align: center;position: fixed; left: 40%;" >
        <img src="979709898.png" width="70" height="70" class="d-inline-block align-top" alt="">
        Costumer Bay
    </a>
        <a id="cart" class=" navbar-brand btn btn-danger" style="position: fixed; left: 70%" >
            <i class="fa fa-shopping-cart fa-3x" aria-hidden="true"></i>
        </a>
    </div>
</nav>
<div id="lin1" class="container-fluid" style="padding: 20px;">

</div>
<div id="lin2" class="container-fluid" style="padding: 20px;">

</div>
</body>
</html>

the jquery.js is the uncompressed raw jquery source code jquery.js是未压缩的原始jquery源代码

Data.Json is in the JS folder Data.Json位于JS文件夹中

data.json: data.json:

[
    {
        "id":0,
        "name":"Laptop",
        "price":50000 ,
        "quantity":0
    },
    {
        "id":1,
        "name":"Phone",
        "price":30000 ,
        "quantity":0
    },
    {
        "id":2,
        "name":"Desktop",
        "price":70000 ,
        "quantity":0
    },
    {
        "id":3,
        "name":"Headphone",
        "price":9000 ,
        "quantity":0
    },
    {
        "id":4,
        "name":"Tablet",
        "price":35000 ,
        "quantity":0
    },
    {
        "id":5,
        "name":"AntiVirus",
        "price":5000 ,
        "quantity":0
    }
]

Now finally the javascript that is not working 现在终于javascript无效了

catalog.js catalog.js

let productinfo=[];
let firsttime = true;

$(function () {
console.log("here");
    refreshcatalog();
    console.log("now where");
    let cart = $('#cart');
    cart.click(function () {          //NOT!WORKING
        loadcart();
    })

});

function refreshcatalog() {
    console.log("no where");
    if (firsttime){
        console.log('evenhere');
        $.getJSON("JS/data.json",function (data) {
            console.log(data);                         //NOT!WORKING
        });
    }
    if(firsttime){
    firsttime=!firsttime;}
}
function loadcart() {
    console.log("will load cart");
}

NOT EVEN CLICK EVENT IS RUNNING. 即使是点击事件也不会运行。 please guide me , NO I dont know AJAX, so please dont use ajax method to solve. 请指导我,不,我不知道AJAX,所以请不要使用ajax方法来解决。

Please help 请帮忙

<----Edit1------> <---- EDIT1 ------>

for all those asking for directory structure:
shopping-----
             |_  catalog.html

             |_  JS(folder) -----
                                 |_  catalog.js
                                 |_  jquery.js
                                 |_  data.json

<---------------------EDIT 2 -__-----------> <--------------------- EDIT 2 -__----------->

Click event started running after changing format to : 更改格式后,Click事件开始运行:

$(document).on('click','#button2', function()
{
    alert("Dynamic button action");
});

A reason why old my old ".click" was not working, will be greatly appreciated .Btw i am using Firefox Developers Edition. 我旧的“.click”无法正常工作的原因,将不胜感激。我正在使用Firefox开发者版。

Below code needs to be changed as your catalog.js & data.json resides in same folder. 下面的代码需要更改,因为catalog.js和data.json位于同一个文件夹中。

$.getJSON("JS/data.json",function (data) 
    console.log(data);
});

Change the code to 将代码更改为

$.getJSON("data.json",function (data) {
    console.log(data);
});

Please check and let me know whether you are able to console the data or not, then will solve the other issue. 请检查并告诉我您是否能够控制数据,然后将解决其他问题。

Lets rearrange the scripts (ordering) 让我们重新排列脚本(订购)

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

I have removed jquery slim, as bootstrap uses that rather I have copied your jquery to the top so that bootstrap can use it. 我已经删除了jquery slim,因为bootstrap使用它而不是我已经将你的jquery复制到顶部,以便bootstrap可以使用它。

Here goes the edited part. 这是编辑过的部分。

Please don't remove JS/data.json from $.getJSON. 请不要从$ .getJSON中删除JS / data.json。

Remove your jquery slim & JS/jquery. 删除你的jquery slim和JS / jquery。 Try using the below CDN link or I have provided in the comments 尝试使用以下CDN链接或我在评论中提供

It worked for me in my local. 它在我当地为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM