开发者

Differing responses to jquery.get in firefox vs chrome

开发者 https://www.devze.com 2023-03-05 00:26 出处:网络
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 n

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');
0

精彩评论

暂无评论...
验证码 换一张
取 消