为什么CORS似乎无法与POST一起使用? [英] Why does CORS not seem to work with POST?

查看:230
本文介绍了为什么CORS似乎无法与POST一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mozilla自己的规范表示简单的 GET POST 应该是本地的CORS,没有预检,但是到目前为止,我所做的每一次 POST 尝试都导致 OPTIONS 标头消失了。当我将其从 POST 更改为获取代码后,会立即发送适当的 GET 请求,因此跨站点部分可以正常工作。

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.

以下是我在firefox中所做的工作的精简示例:

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*/);
            }

这是我已经在chrome和IE中工作的东西:

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


推荐答案

嗯,我不知道所有contentTypes实际上是什么可以,但是文本/纯文本在所有三种浏览器上都可以:

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

但是到目前为止,除了运行成功方法外,我还没有弄清楚什么阻止了请求的执行即使返回505代码也是如此。添加 Access-Control-Allow-Origin的响应标头:* 解决了浏览器不想读取返回数据的问题。

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.

这篇关于为什么CORS似乎无法与POST一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆