[英]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.