Javascript JQuery的Ajax函数在chrome中工作,但在firefox中返回404
我使用一个中央ajax函数向服务器发送ajax Post请求。这是函数的代码:Javascript JQuery的Ajax函数在chrome中工作,但在firefox中返回404,javascript,jquery,ajax,rest,cross-browser,Javascript,Jquery,Ajax,Rest,Cross Browser,我使用一个中央ajax函数向服务器发送ajax Post请求。这是函数的代码: function postJson(url, jsObj, whenSuccess, whenError){ $.ajax({ type: "post", headers: { "Accept": "application/json, text/javascript, */*; q=0.01",
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。当accept或content类型未设置为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上没有
有什么想法吗?试试这个
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); }
}
});
}
提及
读一读试试这个
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); }
}
});
}
提及
阅读删除标题,然后检查。如果我删除标题,那么它在chrome中也会失败?您是否提出了跨域AJAX请求?我没有提出跨域AJAX请求,但在仔细查看chrome标题后,我发现来源:Localhost:。。。。那里REST服务可能不接受没有此标头的请求。。我现在明白了。删除标题,然后检查。如果我删除了标题,那么它在chrome中也会失败?您是否提出了跨域AJAX请求?我没有提出跨域AJAX请求,但在仔细查看chrome标题后,我发现了来源:Localhost:。。。。那里REST服务可能不接受没有此标头的请求。。我现在正在想办法…谢谢,但我需要执行一个POST请求。一点也没有。默认情况下,REST服务器拒绝GET请求。然后您必须在REST文件中添加其他头。您使用哪种语言?我不允许对REST服务进行任何更改。它可能只接受POST请求。。所以让服务接受GET请求是不允许的。谢谢,这很有效。。。我想不会的。为什么会这样?我没有看到任何请求头更改?谢谢,但我需要执行一个POST请求。一点也没有。默认情况下,REST服务器拒绝GET请求。然后您必须在REST文件中添加其他头。您使用哪种语言?我不允许对REST服务进行任何更改。它可能只接受POST请求。。所以让服务接受GET请求是不允许的。谢谢,这很有效。。。我想不会的。为什么会这样?我没有看到任何请求头更改?