2012-10-05 24 views
1

我想從jQuery做一個POST請求到遠程服務器。當我寫這樣的jQuery ajax POST請求與應用程序/ json

$.ajax({ 
      type: 'POST', 
       url: 'http://mysite:8080/orderService/order/send', 
       crossDomain: true, 
       data: JSON.stringify(orderSendRequest), 
       dataType: 'json', 
       success: function(responseData, textStatus, jqXHR) { 
        var value = responseData.someKey; 
       }, 
       error: function (responseData, textStatus, errorThrown) { 
        alert('POST failed.'); 
       } 
     }); 

一切的代碼是好的,但我想ContextType是應用/ JSON,當我這行添加到代碼的要求不工作,我有以下錯誤:

XMLHttpRequest cannot load http://mysite:8080/orderService/order/send. Origin null is not allowed by Access-Control-Allow-Origin. 

$.ajax({ 
      type: 'POST', 
       url: 'http://mysite:8080/orderService/order/send', 
       crossDomain: true, 
       data: JSON.stringify(orderSendRequest), 
       dataType: 'json', 
       contentType : 'application/json; charset=utf-8', 
       success: function(responseData, textStatus, jqXHR) { 
        var value = responseData.someKey; 
       }, 
       error: function (responseData, textStatus, errorThrown) { 
        alert('POST failed.'); 
       } 
     }); 
+0

你是否弄清楚服務器端的非內容類型和內容類型有什麼區別?我認爲,添加dataType時不需要添加內容類型? – devanand

+0

@wkaha contentType是您在調用中發送的數據類型,dataType是回調等待的數據類型。 – Bardo

+0

@Bardo就是這樣。我想我會自動添加。這不是真的 – devanand

回答

0

我不相信json支持crossDomain。研究使用jsonp數據類型。