简体   繁体   中英

How to load json data using AJAX call

I want to load JSON data into HTML file. Problem is, it does not load json data and when I add error event then it gives me error alert for error object. 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 ? Below is my html file which has jquery call to json data with html:

 <!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:

[{
"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

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

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

            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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