Why does CORS not seem to work with POST?

微笑、不失礼 提交于 2019-12-12 13:17:37

问题


Mozilla's own specification says simple GET or POST should be natively CORS's without preflighting but so far every POST attempt I've made has resulted in an OPTIONS header going out. When I change it from POST to get the code immediately sends a proper GET request so the cross site part is working fine.

Here's a slimmed down sample of what I'm doing in 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*/);
            }

Here's what I already had working in chrome and 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);

回答1:


I have the same problem

https://developer.mozilla.org/En/HTTP_Access_Control

says that the enctype has to be text/plain or you need to use Fx4+ All access headers have to be set correctly




回答2:


well, I don't know what all contentTypes actually work but text/plain does on all 3 browsers:

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);

However so far I haven't figured out what's stopping the request from doing anything besides running the success method even when a 505 code is returned. Adding a response header of Access-Control-Allow-Origin: * solved the browser not wanting to read the return data.



来源:https://stackoverflow.com/questions/2563081/why-does-cors-not-seem-to-work-with-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!