简体   繁体   中英

Unable to fetch a value from JSON

I am trying to get a value from the JSON which I am getting through Ajax.

Here is the example of what I am working on http://jsfiddle.net/NNrcp/6/ .

jQuery.ajax({
    url:"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22sta%20India%22&format=json",
    success:function(result){

        jQuery("#div1").html("<pre>"+result+"</pre>");

        console.log(test.query.results.place[0].woeid);
        document.getElementById("jname").innerHTML=test.query.results.place[0].woeid;

    }
});

If I remove the Ajax part and hardcode the JSON, then it works properly but I am unable to make it work using Ajax.

Can someone let me know what I am doing wrong?

Try adding the dataType property to your request

jQuery.ajax({
    url:"..",
    success:function(result){
      ..
    },
    dataType: "json"  // <---
});

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