2013-05-27 16 views
1

我正在使用中央ajax函数向服务器发送ajax Post请求。这是函数的代码:JQuery的Ajax函数在Chrome中工作,但在Firefox中返回404

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); } 
      } 
     }); 
    } 

当我尝试运行我的应用程序工作在铬罚款,但在Firefox它抛出一个404我的REST服务助手返回404时,接受或内容类型ISN “T设置为JSON ...所以我认为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

删除'然后headers'检查。 –

+0

如果我删除头文件,它也会在chrome中失败? – Arninja

+0

你在制作一个跨域AJAX请求吗? – Checksum

回答

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?

http://api.jquery.com/jquery.ajax/

+0

谢谢,但我需要执行一个POST请求。没有得到。 GET请求默认被REST服务器拒绝。 – Arninja

+0

然后你必须在'REST'文件中添加额外的'headers'。你使用哪种语言? –

+0

我不允许对REST服务进行任何更改。它可能只接受POST请求..所以使服务接受GET请求是不允许的。 – Arninja

相关问题