简体   繁体   中英

Filter through JSON array of data using jQuery.ajax

I am trying to set up a function that will filter through JSON array data being brought in from a different domain, but I am running into an error: "Uncaught SyntaxError: Unexpected token :"

This is how things are setup so far:

jQuery.noConflict();

(function($) {
    $(document).ready(function(){

        $.ajax({
            url: 'http://[website-name].org/json/North',
            context: this,
            dataType: "jsonp"
        }).done(function() {
            $.each(data, function(index) {
                alert(data[index].Name);
                alert(data[index].WaitTime);
            });
        });
    });
}(jQuery));

The url contains the JSON data setup in this exact format: {"Name":"Facility Name","WaitTime":"30 min"}

But it seems that the error is pointing to that exact setup. I'm not sure what I'm missing. I'm pretty new to using the jQuery.ajax function, so I may be missing something. The code never seems to make it to the .done part, and I think the JSON data is set up in the correct format (I could be wrong, though).

There are few things wrong in your code. And they are,

.done(function(data) {
   var responseData = JSON.parse(data); //Convert the response string to actual JSON data
   $.each(responseData, function(index) {
      alert(responseData[index].Name);
      alert(responseData[index].WaitTime);

      //Please add a debug point using your devtools and see what is returned as index and act upon that data.
   });
});

In your code it should throw an error cus your are passing a string into the each function.

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