2010-04-01 42 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: *解决了浏览器不想读取返回数据。