不允许选项方法,CORS 到 Google 驱动器
Options method not allowed, CORS to Google Drive
我为 google 驱动器创建了一个脚本,该驱动器 post 到 this document。
我用
调用它
var request = $.ajax({
url: "https://script.google.com/macros/s/AKfycbypnRet5l6gUmoGE8oZV2_6da7fImNU12ejHCHCdOambH7UM2CP/exec",
data: serializedData,
type: "POST",
timeout: 10000,
async: true,
crossDomain: true
});
开始工作
但是,当我在我的本地项目中实现完全相同的代码时,它不起作用。我收到
XMLHttpRequest cannot load
https://script.google.com/macros/s/AKfycbypnRet5l6gUmoGE8oZV2_6da7fImNU12ejHCHCdOambH7UM2CP/exec.
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:3000' is therefore not allowed
access. The response had HTTP status code 405.
405 是 "Method not allowed",当我检查网络流量时,我发现对于我的本地请求,方法是
Request Method:OPTIONS
然而,对于 jsFiddle 中的那个,它是 post 符合预期。我做了一些研究,似乎选项是做交叉源时的预检请求,我不能让它直接使用 post。
non-working OPTIONS 请求有以下 header 不在工作 Fiddle 的请求中:
access-control-request-headers:accept, content-type
access-control-request-method:POST
如果我 post 对我的服务器发出相同的请求(而不是 Google),我看到它包含请求 header Content-Type:application/json; charset=UTF-8
.
jsFiddle在不允许options方法的情况下,如何打通?我能以某种方式让它跳过选项并直接转到 post 吗?
Preflight OPTIONS 请求在请求为 non-simple 时发生,由 non-simple header 或 non-simple HTTP 方法引起。
access-control-request-headers: accept, content-type
header 表示您正在尝试发送 non-simple header。 Accept
总是简单的,但 Content-Type
仅在其值为 application/x-www-form-urlencoded
、multipart/form-data
或 text/plain
时才简单。一定是您的代码(无论出于何种原因)试图为 Content-Type
使用 non-simple 值,并且 Google 未提供 Access-Control-Allow-Headers
响应 header 允许它。
相反,您必须为 Content-Type
指定一个简单的值。您可以通过在 $.ajax 选项 object.
中添加明确的 contentType: "application/x-www-form-urlencoded; charset=UTF-8"
属性 来做到这一点
我为 google 驱动器创建了一个脚本,该驱动器 post 到 this document。
我用
调用它var request = $.ajax({
url: "https://script.google.com/macros/s/AKfycbypnRet5l6gUmoGE8oZV2_6da7fImNU12ejHCHCdOambH7UM2CP/exec",
data: serializedData,
type: "POST",
timeout: 10000,
async: true,
crossDomain: true
});
开始工作
但是,当我在我的本地项目中实现完全相同的代码时,它不起作用。我收到
XMLHttpRequest cannot load https://script.google.com/macros/s/AKfycbypnRet5l6gUmoGE8oZV2_6da7fImNU12ejHCHCdOambH7UM2CP/exec. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.
405 是 "Method not allowed",当我检查网络流量时,我发现对于我的本地请求,方法是
Request Method:OPTIONS
然而,对于 jsFiddle 中的那个,它是 post 符合预期。我做了一些研究,似乎选项是做交叉源时的预检请求,我不能让它直接使用 post。
non-working OPTIONS 请求有以下 header 不在工作 Fiddle 的请求中:
access-control-request-headers:accept, content-type
access-control-request-method:POST
如果我 post 对我的服务器发出相同的请求(而不是 Google),我看到它包含请求 header Content-Type:application/json; charset=UTF-8
.
jsFiddle在不允许options方法的情况下,如何打通?我能以某种方式让它跳过选项并直接转到 post 吗?
Preflight OPTIONS 请求在请求为 non-simple 时发生,由 non-simple header 或 non-simple HTTP 方法引起。
access-control-request-headers: accept, content-type
header 表示您正在尝试发送 non-simple header。 Accept
总是简单的,但 Content-Type
仅在其值为 application/x-www-form-urlencoded
、multipart/form-data
或 text/plain
时才简单。一定是您的代码(无论出于何种原因)试图为 Content-Type
使用 non-simple 值,并且 Google 未提供 Access-Control-Allow-Headers
响应 header 允许它。
相反,您必须为 Content-Type
指定一个简单的值。您可以通过在 $.ajax 选项 object.
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
属性 来做到这一点