Nodejs Express Ajax跨域请求
来源:互联网 发布:js tip 编辑:程序博客网 时间:2024/06/11 03:50
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
//说明下:因为下面要用到res.body获取到前端传过来的参数,用res.body的前提是首先导入body-parser
//跨域处理:
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 , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE,OPTIONS');
if (req.method == 'OPTIONS') {
res.send(200);
}
else {
next();
}
});
//通过这个处理后会允许前端任何方式的跨域请求
//前端页面index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<scriptsrc="//cdn.bootcss.com/jquery/3.2.0/jquery.min.js"></script>
</head>
<body>
<script>
$.ajax({url:'http://localhost:4000/list',
processData: false,
cache:false,
contentType: "application/json; charset=utf-8",
data:JSON.stringify({page:1,pageSize:10,classify_id:77}),
datatype: "json",
type: 'post',
success: function (res) {
console.log(res)
},error:function (res) {
}
})
</script>
</body>
</html>
//后端处理
app.post('/list', function (req, res) {
console.log(req.body)
//这时可以看到控制台输出了:前端传过来的参数{page:1,pageSize:10,classify_id:77}
});
搞定!!!!
- Nodejs Express Ajax跨域请求
- nodejs-express接收ajax请求
- Express 跨域请求
- NodeJS Ajax请求
- nodejs ajax 请求
- nodejs模仿ajax请求
- nodejs跨域请求
- nodejs原生,express,koa跨域问题解决
- nodeJs express允许跨域代码
- nodejs express 获取请求信息的方法
- ajax请求跨域请求
- express框架设置跨域请求
- NodeJS+Express模块的跨域访问控制问题
- nodejs express 允许跨域访问(Access-Control-Allow-Origin)
- nodejs express 允许跨域访问(Access-Control-Allow-Origin)
- nodejs express
- 【NodeJS 】Express
- nodejs--Express
- IE 设置
- encodeURI、decodeURI、encodeURIComponent与decodeURIComponent的区别
- [图像]摄像机标定(2) 张正友标定推导详解
- 打开MacOS中的显示应用窗口预览的功能
- PLSQL工具使用技巧
- Nodejs Express Ajax跨域请求
- Android系统内置应用更新被还原
- iOS 页面之间的传值方式
- react-native 基础开发二 项目结构和基础代码结构
- C++模板实现二叉查找树(四 广度优先遍历)
- windows关于mysql忘记密码的情况下更改密码
- 20170401 oracle format code appears twice
- [图像]摄像机标定(1) 标定中的四个坐标系
- LeetCode 344. Reverse String