2013-05-27 20 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

相關問題