最近学习react.js,自己做练习时总想要使用请求,这次练习我使用的请求方式是fetch,本地后端node.js
fetch(`http://127.0.0.1:8081/login`,{
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method:'post',
body:"username="+username+"&password="+password
}).then(res => res.text()).then(
data =>{
data = JSON.parse(data);
//...
}
)
------------------------------------------------------分割线------------------------------------------------
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.all('*',function(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");