使用node的express框架进行基于cors的get和post跨域
来源:互联网 发布:尤克里里调音器软件 编辑:程序博客网 时间:2024/06/05 08:03
- cors的get跨域
- 1 目录结构
- 2 app90js
- 3 app99js
- 4 9099idxhtml
- 5 用node运行app90js和app99js
- cors的post跨域
- 1 目录结构
- 2 app90js
- 3 app99js
- 4 9099idxhtml
- 5 用node运行app90js和app99js
1 cors的get跨域
1.1 目录结构
1.2 app90.js
var express = require('express')var cors = require('cors')var app = express()app.get('/crosData', cors(), function (req, res, next) { res.json({msg: '这个数据来自9090端口'})})app.listen(9090, function () { console.log('http://127.0.0.1:9090')})
1.3 app99.js
var express = require('express')var app = express()var fs = require('fs')app.get('/',function (req, res, next) { fs.readFile('./idx.html' , 'utf-8' , (err,data)=>{ res.send( data ); })})app.listen(9010, function () { console.log('http://127.0.0.1:9010')})
1.4 /9099/idx.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>按钮</title></head><body> <button id="btn">跨域</button><script> var btn = document.getElementById('btn'); btn.onclick = function(){ var xhr = new XMLHttpRequest(); xhr.open( 'get' , 'http://127.0.0.1:9090/crosData' ); xhr.send(null); xhr.onreadystatechange = function(){ if( xhr.status == 200 && xhr.readyState == 4 ){ console.log( xhr.responseText ); } } }</script></body></html>
1.5 用node运行app90.js和app99.js
然后在浏览器打开
2 cors的post跨域
2.1 目录结构
2.2 app90.js
var express = require('express')var cors = require('cors')var app = express()var bodyParser = require('body-parser')app.use(bodyParser.urlencoded({ extended: true}));app.post('/crosData', cors(), function (req, res, next) { res.json({msg: req.body })})app.listen(9090, function () { console.log('http://127.0.0.1:9090')})
2.3 app99.js
var express = require('express')var app = express()var fs = require('fs')app.get('/',function (req, res, next) { fs.readFile('./idx.html' , 'utf-8' , (err,data)=>{ res.send( data ); })})app.listen(9010, function () { console.log('http://127.0.0.1:9010')})
2.4 /9099/idx.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>按钮</title></head><body> <button id="btn">跨域</button><script> var btn = document.getElementById('btn'); btn.onclick = function(){ var xhr = new XMLHttpRequest(); xhr.open( 'post' , 'http://127.0.0.1:9090/crosData' ); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") xhr.send("name=tom&addr=usa"); xhr.onreadystatechange = function(){ if( xhr.status == 200 && xhr.readyState == 4 ){ console.log( xhr.responseText ); } } }</script></body></html>
2.5 用node运行app90.js和app99.js
然后在浏览器打开
阅读全文
0 0
- 使用node的express框架进行基于cors的get和post跨域
- Node.js+express的get和post输出json
- Node.js+express的接口适配get和post并输出json
- Node express GET和POST获取参数的三种方法
- node.js-express框架的初步使用。
- 使用CORS进行AJAX跨域POST提交
- 基于Express框架使用POST上传文件
- android 基于OkHttp GET 和 POST 的使用
- express,使用get方法和post方法
- okhttp网络框架的使用get和post请求
- nodejs的express获取get和post传值
- express中的get和post请求参数的接收
- node的express框架安装
- Node.js的express框架
- GET和POST的使用
- node 使用get和post向后台请求数据的使用方式对比
- 基于Node.js和express的日志服务器
- 使用Node.js的express框架建立项目
- POJ2299 Ultra-QuickSort(树状数组)
- 设计模式-单例模式
- f_sync解决fatfs文件掉电数据丢失问题
- popwindow
- CSS全屏布局的6种方式
- 使用node的express框架进行基于cors的get和post跨域
- ESP8266固件升级
- 组合数计算(Lucas 逆元 中国剩余定理)
- TensorFlow
- 注解
- FreeRTOS学习笔记二
- PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码)
- 2017.8.19 利用python统计文档中的单词数,行数和字符数
- 多重部分和问题(动态规划(DP))