admin管理员组文章数量:1278918
I have a problem. I built a script to make a request to an internal link that sends back a response. This is what the script looks like:
jQuery.get(callUrl, function(data) {
console.log(typeof data);
jQuery.each(data.items, function(i, item) {
console.log(i);
});
}, 'json');
and the response that the server sends back looks like this:
{"items":[
{
"src": "gallery_item_data_Jc4EaLP6vlwd_large.jpg",
"id": "83",
"gallery_id": "30",
"username": "admin"
}]
}
My problem is when I parse the "data" its type is always string. I need for it to be an object so that I can query it and parse it with my script. To get to the bottom of the problem, I've tried paring my script to the example on jQuery's documentation page:
.getJSON
The main differences with the request on this page and my request is that it uses the getJSON method. When I tried to use that with the url to my server, I have gotten no response at all, so that is the main reason I opted for the get method, and specifying the return type as "json."
Another thing I tried: I checked out the Flickr feed that the jQuery example uses to look for the Content-type header that it sends back, thinking maybe my feed had the wrong header, and it is this on the Flickr feed:
Content-Type application/x-javascript; charset=utf-8
This is exactly the same header on my own feed. So I am puzzled. Does anyone know why this is happening?
I have a problem. I built a script to make a request to an internal link that sends back a response. This is what the script looks like:
jQuery.get(callUrl, function(data) {
console.log(typeof data);
jQuery.each(data.items, function(i, item) {
console.log(i);
});
}, 'json');
and the response that the server sends back looks like this:
{"items":[
{
"src": "gallery_item_data_Jc4EaLP6vlwd_large.jpg",
"id": "83",
"gallery_id": "30",
"username": "admin"
}]
}
My problem is when I parse the "data" its type is always string. I need for it to be an object so that I can query it and parse it with my script. To get to the bottom of the problem, I've tried paring my script to the example on jQuery's documentation page:
http://docs.jquery./Ajax/jQuery.getJSON
The main differences with the request on this page and my request is that it uses the getJSON method. When I tried to use that with the url to my server, I have gotten no response at all, so that is the main reason I opted for the get method, and specifying the return type as "json."
Another thing I tried: I checked out the Flickr feed that the jQuery example uses to look for the Content-type header that it sends back, thinking maybe my feed had the wrong header, and it is this on the Flickr feed:
Content-Type application/x-javascript; charset=utf-8
This is exactly the same header on my own feed. So I am puzzled. Does anyone know why this is happening?
Share Improve this question edited Dec 19, 2019 at 10:35 Mickael Lherminez 6951 gold badge11 silver badges30 bronze badges asked Dec 17, 2008 at 16:44 picardopicardo 24.9k33 gold badges108 silver badges156 bronze badges2 Answers
Reset to default 7The JSON needs to have brackets around it, so it should be:
({"items":[
{
"src": "gallery_item_data_Jc4EaLP6vlwd_large.jpg",
"id": "83",
"gallery_id": "30",
"username": "admin"
}]
})
You should then be able to use getJSON, as this is the easiest way to get the data as an object. However, you can also eval() the string and that will return an object.
Have you tried building an object of that string by calling the JavaScript eval() function? If you first call eval(data) in your callback function it should give you an object.
本文标签: javascriptjQuery not parsing JSON properlyStack Overflow
版权声明:本文标题:javascript - jQuery not parsing JSON properly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741262612a2367898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论