简体   繁体   English

如何使用AJAX调用加载JSON数据

[英]How to load json data using AJAX call

I want to load JSON data into HTML file. 我想将JSON数据加载到HTML文件中。 Problem is, it does not load json data and when I add error event then it gives me error alert for error object. 问题是,它不加载json数据,当我添加错误事件时,它为错误对象提供了错误警报。 Please guide me how I could resolve this issue. 请指导我如何解决此问题。 even, I could not find any specific alert to identiy the error. 甚至,我也找不到任何特定的警报来识别错误。 how to resolve this ? 如何解决呢? Is there any way to get actual error while working with jquery/ajax ? 使用jquery / ajax时,有什么办法得到实际错误? Below is my html file which has jquery call to json data with html: 以下是我的html文件,该文件具有对html的json数据的jquery调用:

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>


<script src="Scripts/jquery-1.7.1.min.js"></script>
<script src="Scripts/jquery-ui-1.8.20.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#btn382").click(function () {
            /* set no cache */
            $.ajaxSetup({ cache: false });

            $.getJSON("car-sale.json", function (data) {
                var html = [];
                alert('result');

            });
        });
    });
</script>

   </head>
 <body>
   <input type="submit" id="btn382" />
  </body>
   </html>

JSON file is as follow: JSON文件如下:

[{
"Manufacturer": "Toyota",
"Sold": 1200,
"Month": "2012-11"
}, {
"Manufacturer": "Ford",
"Sold": 1100,
"Month": "2012-11"
}, {
"Manufacturer": "BMW",
"Sold": 900,
"Month": "2012-11"
}, {
"Manufacturer": "Benz",
"Sold": 600,
"Month": "2012-11"
}, {
"Manufacturer": "GMC",
"Sold": 500,
"Month": "2012-11"
}, {
"Manufacturer": "HUMMER",
"Sold": 120,
"Month": "2012-11"
}]

in the page header you should define this 在页面标题中,您应该定义此

<script src="car-sale.json"></script>

i check this with a firefox and its working fine check this with firefox 我用firefox检查它,并且工作正常,用firefox检查

$.getJSON('car-sale.json', function (data) {

                 $.each( data, function( key, val ) {
                console.log(val.Manufacturer );
                });

            });

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

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