Differing responses to jquery.get in firefox vs chrome
I'm trying to pull JSON data from another source开发者_运维百科 by using the jquery.get method. Unfortunaltey, Firfox 4 and Chrome are giving me different responses. In Firefox I get a string which needs to be parsed, in chrome, I get parsed JSON. Why the difference and how do I avoid it?
//works in Firefox
$.get(url, query, function(resp){
var data = $.parseJSON(resp)
var hits = data.hits.hits
}
//works in Chrome
$.get(url, query, function(resp){
var hits = resp.hits.hits
}
It would be better to be explicit: tell jQuery that you're expecting JSON to be returned. This should ensure consistent behaviour.
$.get(url, query, function(resp){
var hits = resp.hits.hits
}, 'json');
精彩评论