尝试发送 POST 请求时,应用脚本发送 405 响应 [英] App Script sends 405 response when trying to send a POST request
问题描述
我已经使用 doPost 方法公开(任何人,甚至匿名)发布了一个应用脚本,如下所示,
I have published an app script publicly (Anyone, even anonymous) with a doPost method as follow,
function doPost(e){
var sheet = SpreadsheetApp.getActiveSheet();
var length = e.contentLength;
var body = e.postData.contents;
var jsonString = e.postData.getDataAsString();
var jsonData = JSON.parse(jsonString);
sheet.appendRow([jsonData.title, length]);
var MyResponse = "works";
return ContentService.createTextOutput(MyResponse).setMimeType(ContentService.MimeType.JAVASCRIPT);
}
当我使用 Advanced Rest Client 发送带有 JSON 对象的 Post 请求时,它一切正常并返回 200 OK 响应.但是,当我尝试从本地托管的 react 应用程序发送带有 react axios 的发布请求时,它会发送 405 响应.
When I sent a Post request with a JSON object with Advanced Rest Client it all works and return a 200 OK response. But when I try to send a post request with the react axios from a locally hosted react app it sends a 405 Response.
XMLHttpRequest cannot load https://script.google.com/macros/s/AKfycbzyc2CG9xLM-igL3zuslSmNY2GewL5seTWpMpDIQr_5eCod7_U/exec. Response for preflight has invalid HTTP status code 405
我也在浏览器中启用了跨源资源共享.发送POST请求的函数如下,
I have enabled cross origin resource sharing in the browser as well. The function that sends the POST request is as follow,
axios({
method:'post',
url:'https://script.google.com/macros/s/AKfycbzyc2CG9xLM-igL3zuslSmNY2GewL5seTWpMpDIQr_5eCod7_U/exec',
data: {
"title": 'Fred',
"lastName": 'Flintstone'
}
}).then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
推荐答案
你错过了重要的部分:
预检响应包含无效的 HTTP 状态代码 405.
Response for preflight has invalid HTTP status code 405.
您的浏览器正在发出 预检请求,它使用 OPTIONS
HTTP 方法.这是为了检查服务器是否允许 POST
请求 –405
状态代码是在对 OPTIONS
请求的响应中发送的,而不是您的 POST
请求.
Your browser is making a preflight request, which uses the OPTIONS
HTTP method. This is to check whether the server will allow the POST
request – the 405
status code is sent in the response to the OPTIONS
request, not your POST
request.
CORS 预检请求是一个 CORS 请求,用于检查 CORS 协议是否 来源
此外,对于可能对服务器数据造成副作用的 HTTP 请求方法(特别是对于
GET
,或用于 POST
用于某些 MIME 类型),规范要求浏览器预检"请求,使用 HTTP OPTIONS
请求方法,然后,在服务器批准"后,使用实际的 HTTP 请求方法发送实际请求. 来源有些请求不会触发 CORS 预检.这些在本文中被称为简单请求"[...] 来源
本文部分详细说明了请求必须满足的条件才能被视为简单请求".
[...] 预检"请求首先通过
发送 HTTP 请求OPTIONS
方法到其他域上的资源,以确定实际请求是否可以安全发送.跨站点请求是这样预检的,因为它们可能对用户数据有影响. 来源本文部分详细介绍了导致请求被预检的条件.
A CORS preflight request is a CORS request that checks to see if the CORS protocol is understood. Source
Additionally, for HTTP request methods that can cause side-effects on server's data (in particular, for HTTP methods other thanGET
, or forPOST
usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTPOPTIONS
request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Source
Some requests don’t trigger a CORS preflight. Those are called "simple requests" in this article [...] Source
This article section details the conditions a request has to meet to be considered a "simple request".
[...] "preflighted" requests first send an HTTP request by theOPTIONS
method to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data. Source
This article section details the conditions which cause a request to be preflighted.
在这种情况下,以下是导致请求被预检的原因:
In this case, the following is causing the request to be preflighted:
[...] 如果 Content-Type
标头的值不是以下值:
[...] if the
Content-Type
header has a value other than the following:
application/x-www-form-urlencoded
multipart/form-data
文本/纯文本
Content-Type
标头的值被 axios 设置为 application/json;charset=utf-8
.使用 text/plain;charset=utf-8
或 text/plain
可以解决问题:
The value for the Content-Type
header is set to application/json;charset=utf-8
by axios. Using text/plain;charset=utf-8
or text/plain
fixes the problem:
axios({
method: 'post',
url: 'https://script.google.com/macros/s/AKfycbzyc2CG9xLM-igL3zuslSmNY2GewL5seTWpMpDIQr_5eCod7_U/exec',
data: {
title: 'Fred',
lastName: 'Flintstone',
},
headers: {
'Content-Type': 'text/plain;charset=utf-8',
},
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
这篇关于尝试发送 POST 请求时,应用脚本发送 405 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!