我正在使用中央ajax函数将ajax Post请求发送到服务器。这是函数的代码:
function postJson(url, jsObj, whenSuccess, whenError){
$.ajax({
type: "post",
headers: {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Content-Type": "application/json, text/javascript, */*; q=0.01"
},
dataType: "json",
url: url,
data: JSON.stringify(jsObj),
success: function(result){
if(whenSuccess !== undefined){ whenSuccess(result); }
},
error: function(xhr){
if(whenError !== undefined){ whenError(xhr.status); }
}
});
}
当我尝试运行我的应用程序时,它在chrome中工作正常,但在firefox中它会抛出404.当接受或内容类型未设置为JSON时,我的REST服务助手返回404 ...所以我认为firefox可能不会添加标题,但是当我查看发送的请求时:
Request URL:
http://localhost:9081/api/1/localize/validation.json
Request Method:
POST
Status Code:
HTTP/1.1 404 Not Found
Request Headers
08:40:10.000
X-Requested-With:XMLHttpRequestUser-Agent......
Referer:http://localhost:9081/kportal/
Pragma:no-cacheHost:localhost:9081
Content-Type:application/json, text/javascript; charset=UTF-8, */*; q=0.01
Content-Length:2
Connection:keep-alive
Cache-Control:no-cache
Accept-Language:en-US,en;q=0.5
Accept-Encoding:gzip, deflate
Accept:application/json, text/javascript, */*; q=0.01
您可以看到已设置必要的标头。我仍然在Firefox中获得404而不是Chrome。
有什么想法吗?
答案 0 :(得分:3)
试试这个,
function postJson(url, jsObj, whenSuccess, whenError){
$.ajax({
type: "post",
contentType: "application/json; charset=utf-8",
accepts: {
xml: 'text/xml',
text: 'text/plain'
},
dataType: "json",
url: url,
data: JSON.stringify(jsObj),
success: function(result){
if(whenSuccess !== undefined){ whenSuccess(result); }
},
error: function(xhr){
if(whenError !== undefined){ whenError(xhr.status); }
}
});
}
参考What is the point of jQuery ajax accepts attrib? Does it actually do anything?