简体   繁体   English

Chrome并不总是发送POST请求

[英]Chrome doesn't always send POST request

I am using 'fetch' to make a post call to my node server. 我正在使用“提取”对我的节点服务器进行后期调用。 I am trying to do this on click of a button. 我试图通过单击按钮来执行此操作。

When I click the button an OPTIONS request is sent but the browser doesn't send the subsequent POST call. 当我单击按钮时,将发送OPTIONS请求,但浏览器不会发送后续的POST调用。

On multiple clicks, browser keeps sending Options request and just once in many attempts does Post request gets sent out. 多次单击时,浏览器将继续发送“选项”请求,而多次尝试中仅发送一次“发布”请求。

let data = {name: "Butternut cucumberpatch", age: 26}
let headers = new Headers()
headers.append("Content-type", "application/json")

fetch(join(apiPath, "/user"), {
  method: "POST",
  headers: headers,
  body: JSON.stringify(data)
}).then(response => response)

(1) The code work's on my colleague's chrome browser, just not mine. (1)代码工作在我同事的Chrome浏览器中,而并非我的。 The chrome version is the same. chrome版本是相同的。 (2) This works perfectly fine on POSTMAN, Safari. (2)这在POSTMAN,Safari上运行正常。 (3) I thought maybe there is an issue with native fetch so I tried using 'fetch', 'unfetch', 'request' modules. (3)我以为本地获取可能存在问题,因此我尝试使用“获取”,“取消获取”,“请求”模块。 But still faced the same issue. 但是仍然面临着同样的问题。

I found a solution here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests 我在这里找到了解决方案: https : //developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests

Since my POST request was a cross origin request, the request preflight was happening. 由于我的POST请求是跨源请求,因此发生了请求预检。 I changed my Content-Type to be application/x-www-form-urlencoded 我将Content-Type更改为application / x-www-form-urlencoded

If I send out the request with Content-Type : application/x-www-form-urlencoded, the POST request happens every time. 如果我发出Content-Type的请求:application / x-www-form-urlencoded,则每次都会发生POST请求。 I still don't know why it (application/json) works on my colleague's chrome browser though and not mine. 我仍然不知道为什么它(application / json)在我的同事的chrome浏览器上可以工作,而不是我的。 I want to make it work with application/json. 我想使其与application / json一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 chrome 发送帖子请求无法获取请求正文 - chrome send post request can't get request body 无法从 Chrome 扩展程序向 REST API 发送 POST 请求 - Can't send POST request to a REST API from a Chrome extension jQuery不会发送内容类型为application / json的发布请求 - jQuery doesn't send post request with content type application/json IE 11不向Web应用程序发送CORS'POST'请求 - IE 11 doesn't send CORS 'POST' request to the web application 通过chrome扩展程序中的post request发送URL onload - send the URL onload by post request in chrome extension 从Chrome扩展程序发送POST请求 - Send POST request from Chrome Extension jQuery.post() 发送电子邮件但不会在没有服务器刷新的情况下到达 .done()/.fail()/.always() - jQuery.post() send email but doesn't gets to .done()/.fail()/.always() without server refresh 游标:无; 并不总是适用于 chrome - cursor: none; doesn't always work on chrome Ionic中的发布请求在$ _POST中不存在 - Post Request in Ionic doesn't exist in $_POST 提取POST请求不会返回使用res.send('sampletext')发送的响应。 - Fetch POST Request doesn't return response sent using res.send('sampletext');
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM