当前位置: 首页 > news >正文

推广员网站怎么做北京百度总部电话

推广员网站怎么做,北京百度总部电话,凡科做的是网站吗,大连最繁华的区是哪个区CORS 的使用 创建两个服务器,进入对应目录,命令行 node server.js,node server2.js,启动服务器。 server.js 会读取 test.html,在8888端口显示,test.html发送跨域请求8887服务器,server2.js通过…

CORS 的使用

创建两个服务器,进入对应目录,命令行 node server.js,node server2.js,启动服务器。

server.js 会读取 test.html,在8888端口显示,test.html发送跨域请求8887服务器,server2.js通过设置'Access-Control-Allow-Origin': 'http://127.0.0.1:8888'允许跨域。

浏览器认为localhost和127.0.0.1不同,所以本地用127.0.0.1

// server.js
const http = require('http')
const fs = require('fs')http.createServer(function (request, response) {console.log('request come', request.url)const html = fs.readFileSync('test.html', 'utf8') // 注意设置 utf8,这样读取的是字符串,否则是二进制的数据response.writeHead(200, {'Content-Type': 'text/html' // 不设置的话,浏览器会直接显示,不解析;默认浏览器会加上})response.end(html)
}).listen(8888)console.log('server listening on 8888')
复制代码
// server2.js
const http = require('http')http.createServer(function (request, response) {console.log('request come', request.url)response.writeHead(200, { // 不设置这个头,浏览器还是会发送请求,并接收内容,浏览器检查head头,如果没有'Access-Control-Allow-Origin',并且设置为允许,浏览器会把本次请求返回的内容会略掉,并且报错。'Access-Control-Allow-Origin': 'http://127.0.0.1:8888', // 允许 8888 的域名访问,*也可以(不够安全)})response.end('123')
}).listen(8887)console.log('server listening on 8887')
复制代码

添加多个头

通过判断 request.url 来判断是否添加不同的 head 头。

JSONP

浏览器允许 link img script中的src或href进行跨域请求,JSONP的原理就是在script标签中,加载链接,链接访问了服务器某个请求,并返回了内容,服务器返回的内容是可控的,可以在服务器返回内容中的script标签中写一段可执行的JS代码,然后调用JSONP发起请求的一些内容。

// test.html
<script src="http://127.0.0.1:8887/"></script>
复制代码

这样不设置8887的head头,也可以进行跨域,得到请求内容。

CORS跨域限制

主要包括 methods content-type 和 请求头的限制。

允许方法

默认允许方法 GET HEAD POST,其他的方法 PUT DELETE 默认是不允许的。

允许Content-Type

text/plain

multipart/form-data

application/x-www-form-urlencoded

除了这三种,其他的 Content-Type 也需要使用预请求验证后,才能发送。

其他限制

  • 请求头限制,自定义的请求头默认是不允许的,也需要验证;官方文档,关于请求头的详细信息https://fetch.spec.whatwg.org/#cors-safelisted-request-header
  • XMLHttpRequestUpload 对象均没有注册任何事件监听器(很少用)
  • 请求中没有使用 ReadableStream 对象(很少用)

CORS预请求

demo

8888下的 test.html 发送的请求携带了自定义请求头,从浏览器看请求发送成功,返回状态码200,但是浏览器会报错,不允许跨域,数据虽然返回,但被浏览器忽略了。

// server.js
const http = require('http')
const fs = require('fs')http.createServer(function (request, response) {console.log('request come', request.url)const html = fs.readFileSync('test.html', 'utf8') response.writeHead(200, {'Content-Type': 'text/html' })response.end(html)
}).listen(8888)console.log('server listening on 8888')
复制代码
// server2.js
const http = require('http')http.createServer(function (request, response) {console.log('request come', request.url)response.writeHead(200, { 'Access-Control-Allow-Origin': '*'})response.end('123')
}).listen(8887)console.log('server listening on 8887')
复制代码
<script>fetch('http://localhost:8887', { // 发送请求method: 'POST',headers: { // 一个自定义的头'X-Test-Cors': '123'}})
</script>
复制代码

实现预请求

const http = require('http')http.createServer(function (request, response) {console.log('request come', request.url)response.writeHead(200, { 'Access-Control-Allow-Origin': '*','Access-Control-Allow-Headers': 'X-Test-Cors' // 服务端添加允许自定义请求头})response.end('123')
}).listen(8887)console.log('server listening on 8887')
复制代码

查看network,会发现多了一个请求文件localhost,它的 Request Method 请求方法是 OPTIONS,其他的很正常请求一样,通过 OPTIONS 请求获得服务端允许发送请求的认可。之后再实际发送 POST 请求。

允许其他请求方法

const http = require('http')http.createServer(function (request, response) {console.log('request come', request.url)response.writeHead(200, {'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'X-Test-Cors','Access-Control-Allow-Methods': 'POST, PUT, DELETE' // 允许使用被限制的请求方法})response.end('123')
}).listen(8887)console.log('server listening on 8887')
复制代码

跨域请求时间

比如第一次请求,network观察文件,会有 Method 是 OPTIONS 的预请求文件,再次刷新网页发送请求,这个文件就不会再发送了,不会出现在 network 列表中。

const http = require('http')http.createServer(function (request, response) {console.log('request come', request.url)response.writeHead(200, {'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'X-Test-Cors','Access-Control-Allow-Methods': 'POST, PUT, DELETE','Access-Control-Max-Age': '1000' // 1000s之内,不需要再发送预请求进行验证了,时间内直接发正式请求})response.end('123')
}).listen(8887)console.log('server listening on 8887')
复制代码

浏览器希望通过限制保证服务端的安全

转载于:https://juejin.im/post/5ba59cf25188255c46368578

http://www.lbrq.cn/news/2500345.html

相关文章:

  • 国内最好的erp系统网络推广seo是什么
  • 贵阳电商网站建设美国婚恋网站排名
  • 恶搞网站怎么做什么是电商?电商怎么做
  • 做网站的工具传播易广告投放平台
  • 北京网站建设新闻网络营销的推广方法
  • 仙桃有哪些做网站的公司seo搜索引擎入门教程
  • 余姚做网站哪家好开封网站快速排名优化
  • node.js 做网站百度贴吧人工客服电话
  • 本地网站怎么做上海网站设计
  • 深圳模板网站建设公推广平台有哪些渠道
  • 如何扫描一个网站的漏洞百度网页版入口链接
  • iis5.1发布网站百度搜索网站排名
  • 做网站卖房写标题网站运营公司
  • 做加盟的网站百度竞价开户流程
  • 网站维护的意义大数据营销系统怎么样
  • 做电商网站微信号是多少口碑营销什么意思
  • wordpress动静分离oss百度seo排名点击
  • 贵阳网站制作方舟网络现在最火的推广平台有哪些
  • 免费访问国外网站的appcnzz统计
  • 重庆相册制作工厂哈尔滨百度关键词优化
  • 在线做维恩图的生物信息学网站怎样进入12345的公众号
  • 在南海建设工程交易中心网站百度学术论文查重官网
  • 哪里可以做网站的sem是什么职位
  • 有没有便宜的网站制作seo外包优化网站
  • 无锡网站建设培训新浪微博指数查询
  • web购物网站模板下载seo管家
  • 学做网站学费谷歌商店下载官网
  • 建设网站必备的开发工具南宁做网站公司
  • 网站做镜像百度游戏中心
  • 微信制作宣传网站有哪些内容今日十大新闻
  • mmap的调用层级与内核态陷入全过程
  • Linux驱动21 --- FFMPEG 音频 API
  • Java面试宝典:MySQL执行原理二
  • bash的特性-常用的通配符
  • 【Qt开发】信号与槽(二)-> 信号和槽的使用
  • 自动标注软件X-AnyLabeling的使用教程