2012-12-04 43 views
0

我已閱讀了很多有關CORS的主題& Javascript和關於更改您的文章中的標題,但我找不到正確的示例,我正在尋找。jQuery POST到web服務通過CORS

所以我打算先上去開始說明情況:

  • ,因爲這是我夠不着的地方我也沒有什麼改變的網絡服務器(這是一個SAP雲門戶網站)
  • 我只能更改POST代碼,所以我只能控制我發送的內容。

我的問題是在下面的帖子中描述: jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox

- >我的FF &鉻頭髮送方法選項,而不是POST方法。

我寫的示例代碼,在IE瀏覽器而不是在FF &工程鉻:

var dataString = "<result><firstname>example</firstname><lastname>ThisIsSparta</lastname></result>"; 
    var urlString = "http://delyo001.you.local:8000/sap/bc/youconsulting/ws/rest/anonymous/z_names_post"; 

    //Add TO SAP. 
    var aData = 
      jQuery.ajax({ 
       type: "POST", 
       contentType: "application/xml", 
       url: urlString, // for different servers cross-domain restrictions need to be handled 
       data: dataString, 
       dataType: "text", 
       success: function(xml) { // callback called when data is received 
        //oModel.setData(data);    // fill the received data into the JSONModel 
        alert("success to post"); 
       }, 

       error: function(xml) { // callback called when data is received 
        //oModel.setData(data);    // fill the received data into the JSONModel 
        alert("fail to post"); 
       } 
      }); 
     }); 

或者

var invocation = new XMLHttpRequest(); 
var url = 'http://delyo001.you.local:8000/sap/bc/youconsulting/ws/rest/anonymous/z_names_post'; 
var body = '<result><firstname>perthyrtyrtygop</firstname><lastname>sparta</lastname></result>'; 


    invocation.open('POST', url, true); 
    invocation.setRequestHeader('X-PINGOTHER', 'pingpong'); 
    invocation.setRequestHeader('Content-Type', 'application/xml'); 
    invocation.send(body); 

我發現2種方法來解決這個問題,但沒有任何例子: - 用代理做些什麼? - 發送特定的頭

我的問題的更多信息可以在這裏找到: - http://scn.sap.com/message/13697625#13697625

+1

我需要找到一種方法,通過代理來做到這一點:) – Vinozio

+1

選項請求是CORS「預檢要求」,發送,因爲您使用的是少見'內容Type'頭,這使得它一個「非簡單」的CORS請求。非簡單的請求要求瀏覽器向服務器發送一個OPTIONS請求來驗證動詞和頭部是否是預期的。我在[這個答案]中討論非簡單請求(http://stackoverflow.com/questions/13400594/understanding-xmlhttprequest-over-cors-responsetext/13400954#13400954),它也鏈接到[HTML5 Rocks的CORS頁面](http://www.html5rocks.com/en/tutorials/cors/)。 – apsillers

回答

1

如果不能設置在服務器端的右頭,你不能修改JSONP響應你應該確實使用代理。

代理腳本是一種中間件。您向腳本請求獲取數據的腳本,並將其返回給您。例如php proxy。你可以在asp,jsp,flash甚至java applet中做同樣的事情。

現在,您已將SAP服務,代理(php)文件放在您的首選位置,並將您的本地JavaScript與代理位於同一個域中。你甚至不需要CORS。

如果你想把代理放在另一個域中,你必須確保php文件發送正確的頭文件。 (Access-Control-Allow-Origin yourdomainAccess-Control-Allow-Origin * for allow all

+0

你好VDP, 感謝您的回覆! 這確實是正確的路要走,我會嘗試找到一些我可以編輯的Javascript代碼。 再次感謝! – Vinozio