AJAX跨域POST发送json时,会先发送一个OPTIONS预请求
<a href="http://blog.163.com/sdhhqb@126/blog/static/637055372016112591913960/">原文链接</a>
var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: false })); // for parsing application/x-www-form-urlencoded
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
var originalUrl = req.originalUrl;
option = {
url: 'http://algo.chiq-cloud.com/chiqalgo'+originalUrl,
method: "POST",
json: true,
body: req.body,
headers: {
"content-type":"application/json"
}
};
var method = req.method;
// OPTIONS请求直接返回成功
if (method == 'OPTIONS') {
res.status(200).send('{"test": "options ok"}');
return;
}
if (method == 'POST') {
option.method= "POST";
}
console.log(req.ip + ' ' + method + ' ' + originalUrl + ' ' +