admin管理员组文章数量:1415696
I'm not a front-end developer, and I'm working on a casual project, which requires AJAX getting a piece of JSON.
$('#btn1').click(function() {
$.ajax({
url: '',
type: "get",
success: function(response, textStatus, jqXHR){
var age1 = JSON.parse(response).data.age; // works for Firefox
var age2 = response.data.age; // works for Chrome
},
error: function(jqXHR, textStatus, errorThrown){
console.log('Error')
},
plete: function(){
console.log('Complete')
}
});
});
I use Firebug in Firefox and Developer Tools in Chrome. The response
is of type Object
in Chrome, and is a String
in Firefox.
Is there a generic way to do AJAX in jQuery and successfully parse the response?
I'm not a front-end developer, and I'm working on a casual project, which requires AJAX getting a piece of JSON.
$('#btn1').click(function() {
$.ajax({
url: 'http://mywebsite./persons/mike',
type: "get",
success: function(response, textStatus, jqXHR){
var age1 = JSON.parse(response).data.age; // works for Firefox
var age2 = response.data.age; // works for Chrome
},
error: function(jqXHR, textStatus, errorThrown){
console.log('Error')
},
plete: function(){
console.log('Complete')
}
});
});
I use Firebug in Firefox and Developer Tools in Chrome. The response
is of type Object
in Chrome, and is a String
in Firefox.
Is there a generic way to do AJAX in jQuery and successfully parse the response?
Share Improve this question asked Feb 15, 2013 at 2:29 zihaoyuzihaoyu 5,72111 gold badges44 silver badges47 bronze badges 5- Your server is probably not setting the proper response headers to make jQuery understand it's JSON in both browsers. – bfavaretto Commented Feb 15, 2013 at 2:32
-
@bfavaretto the server side uses Jersey and the endpoint is annotated
@Produces(MediaType.APPLICATION_JSON)
. – zihaoyu Commented Feb 15, 2013 at 2:36 - Weird, the Intelligent Guess should parse it in Firefox then. Maybe check the response headers from Firebug's Net tab to see if the browser actually receives that header. Nevertheless forcing the response to be parsed as JSON will take care of the issue. – Fabrício Matté Commented Feb 15, 2013 at 2:39
-
1
@FabrícioMatté I just looked. Its
Content-Type
isapplication/json
, but theresponse
is a string if I ment outdataType: 'json'
. I'm going to keep thedataType
option in$.ajax
. Thanks. – zihaoyu Commented Feb 15, 2013 at 2:45 -
Ok.
=]
Might be a bug withjQuery.ajax
's Intelligent Guess then. – Fabrício Matté Commented Feb 15, 2013 at 2:47
1 Answer
Reset to default 7Set dataType: 'json'
in your $.ajax
.
This will force the response inside the success
/done
handler to be an already parsed object, else, in case the response is not valid JSON, the error
/fail
handler will be called.
Note: Using dataType
will force the response to be parsed as JSON even if you don't specify a Content-Type: application/json
header in the response.
If you specify the header though, jQuery.ajax
's Intelligent Guess would parse the response as an object even if you don't specify the dataType
.
So, you can either use the dataType: 'json'
in $.ajax
or output the header Content-Type: application/json
from the back-end and it will work fine. Or both if you want to be throughout. =]
本文标签: javascriptjquery ajax response difference between Chrome and FirefoxStack Overflow
版权声明:本文标题:javascript - jquery ajax response: difference between Chrome and Firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745237521a2649124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论