2010-04-01 41 views
3

Mozilla的自己specification說簡單GETPOST應該是本地CORS的無預檢但到目前爲止一切POST嘗試我做已經導致了OPTIONS頭走出去。當我將其從POST更改爲代碼立即發送一個適當的GET請求,以便跨網站部分工作正常。爲什麼CORS似乎不適用於POST?

下面是我在做什麼在Firefox中精簡下來的樣品:

var destinationUrl = 'http://imaginarydevelopment.com/postURL'; 
var invocation = new XMLHttpRequest(); 
      if (invocation) { 
       invocation.open('POST', destinationUrl, true); 
       //tried with and without this line 
       //invocation.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

       invocation.onreadystatechange = (function Handler() { 
       if (invocation.readyState == 4) 
         alert('Request made'); 
       }); 
       invocation.send(/* tried with and without data*/); 
      } 

這裏是我已經在Chrome和IE工作:

var destinationUrl = 'http://imaginarydevelopment.com/postURL'; 
var destination = { url: destinationUrl, type: 'POST', success: AjaxSuccess, error: AjaxError, 
      dataType: 'text', contentType: 'application/x-www-form-urlencoded' 
     }; 
    destination.data = { 'rows': rowList, 'token': token }; 
      $jq.ajax(destination); 

回答

0

好了,我不知道什麼都CONTENTTYPES實際工作,但text/plain確實在所有3個瀏覽器:

var destination = { url: destinationUrl, type: 'POST', success: AjaxSuccess, error: AjaxError, 
      contentType: 'text/plain' 
     }; 
var postData={ 'anArray': theArray, 'token': token }; 
      destination.data=JSON.stringify(postData); 

$jq.ajax(destination); 

但是這樣至今我還沒有弄清楚什麼阻止了請求,除了運行成功方法之外,即使返回了505代碼,也不做任何事情。添加一個響應頭Access-Control-Allow-Origin: *解決了瀏覽器不想讀取返回數據。