尝试发送 POST 请求时,App 脚本发送 405 响应 [英] App Script sends 405 response when trying to send a POST request

查看:49
本文介绍了尝试发送 POST 请求时,App 脚本发送 405 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经公开发布了一个应用脚本(任何人,甚至匿名),使用 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 的 post 请求时,它会发送 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 than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS 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 the OPTIONS 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
  • text/plain

Content-Type 标头的值由 axios 设置为 application/json;charset=utf-8.使用 text/plain;charset=utf-8text/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 请求时,App 脚本发送 405 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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