美文网首页
CORS and JSONP

CORS and JSONP

作者: JSL_FS | 来源:发表于2018-02-11 11:02 被阅读0次

reference teacher run

same-origin policy:

1.protocol
2.domain name
3.port

AJAX

can only send message to same-origin site (diferent with JSONP、WebSocket、CORS)

JSONP

send request by script tag

function addScriptTag(src) {
  var script = document.createElement('script');
  script.setAttribute("type","text/javascript");
  script.src = src;
  document.body.appendChild(script);
}

window.onload = function () {
  addScriptTag('http://example.com/ip?callback=foo');
}

function foo(data) {
  console.log('Your public IP address is: ' + data.ip);
};

WebSocket(a communication protocol )

// config the request origin , from which domain
//request head
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com  // key field

//response head
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

CORS ( Cross-origin resource sharing ): a W3C standard

//front end 
var xhr = new XMLHttpRequest()
xhr.withCredentials = true

GET /cors HTTP/1.1
Origin: http://api.bob.com


//server side
Access-Control-Allow-Origin: http://api.bob.com
Access-Control-Allow-Credentials: true

JSONP can only suports GET method , it suport old explorers and sites don't surport CORS

CORS suport all HTTP requests , it suports IE10+

相关文章

  • spring boot CORS 支持

    一、Web 开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS 等等 CORS 与 JSONP...

  • 解决跨域请求的几种常用方式

    总结: jsonp / iframe / window.name / cors / img.src jsonp ...

  • 跨域

    1:CORS2:JSONP

  • Ajax下

    一、cors跨域请求 二、jsonp百度搜索的例子 jsonp.html jsonp.js

  • 跨域

    1、Json jsonp 只能支持 get 请求; 2、cors cors 可以支持多种请求。cors 并不需要前...

  • 跨域【详解】

    本篇有四种方法跨域:CORS、JSONP、降域、window.postMessage() 1. CORS CORS...

  • Ajax跨域请求_cors

    cors 内容回顾:restful 规范10个 除了 jsonp(它只可以发 get 请求), 还有cors(可以...

  • 跨域

    解决跨域访问,最常用有三种办法 CORS jsonp 代理 CORS cross origin resource ...

  • CORS and JSONP

    reference teacher run same-origin policy: 1.protocol2.do...

  • JSONP && CORS

    跨域必须先知道同源策略。 JSONP html中script标签可以引入其他域下的js,比如引入线上的jquery...

网友评论

      本文标题:CORS and JSONP

      本文链接:https://www.haomeiwen.com/subject/rsdktftx.html