English 中文(简体)
Why does CORS not seem to work with POST?
原标题:

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);
最佳回答

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.

问题回答

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





相关问题
Output W3C compliant XHTML from image slices

When slicing and saving for web in Photoshop CS4, the HTML layout output by Photoshop is done using tags, which is not what we want. Is there a way to get Photoshop to output W3C compliant tableless ...

HTML validator for Win32?

Does anybody know a fast, free local HTML validator to copy-and-paste code in, that shows errors directly on their respective lines?

Best Way to View Generated Source of Webpage?

I m looking for a tool that will give me the proper generated source including DOM changes made by AJAX requests for input into W3 s validator. I ve tried the following methods: Web Developer Toolbar ...

W3C Compliance Testing: Methodology

We are going to redesign a large web service which has around 25 unique page templates delivering thousands of pages. One of the major requirements is w3c compliance (html 4.01 transitional / WCAG1.0)....

热门标签