When sending a post request to the background, there is only one options request, but no real post request. The get request can succeed. And if you use postman, the post request can also be successful. Do I need to configure anything? code show as below:
When sending a post request:
Send get request:
In the postman environment, post can be successful.
The nodejs code is as follows:
var app = require('express')();
var User = require("./users.js");
app.post('/users/login',function (req,res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.send("foo");
console.log(res)
})
app.get('/users/login',function (req,res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.send("bar");
})
app.listen('1090','127.0.0.1');
The front desk uses angular’s $http.
What I want to ask is, why are there only these cross-domain options when posting?
首先搞清楚为啥会发一个
options
的请求(如果你已经知道了就忽略好了),以下是发送(Preflight request)的先决条件。由于GET请求没有预请求,直接发送所以没问题。你在后端需要处理的
options
的请求,并且带上CORS需要的那些头,例如Access-Control-Allow-Origin
等,这样options
的预请求成功之后,才会接着发送真正的post
请求!!!我自己有一个疑问,我看你后端的代码是没有处理
options
的请求的,但是你前端的状态显示这个请求是成功的!!!想知道这个是怎么弄的?希望能帮到你
https://github.com/wxungang/n...
完整代码