JSONP(JSONP - JSON with Padding是JSON的一种“使用模式”),利用script标签的src属性(浏览器允许script标签跨域)
==>
>
=>
===>
<script>
{
var tag = document.createElement(
'script');
tag.src =
":8000/test/";
document.head.appendChild(tag);
document.head.removeChild(tag);
}
{
$.ajax({
url:
":8000/test/",
type:
'GET',
dataType:
'JSONP',
success:
function(data, statusText, xmlHttpRequest){
console.log(data);
}
})
}
>
基于JSONP实现跨域Ajax - Demo
![](http://www.h5cn.com/upload8/allimg/160813/150109D51_1.png)
tornado.ioloop
import tornado.web
:
:
self.render(
'index.html')
:
self.write(
't1.post')
settings={
'template_path':
'views',
'static_path':
'statics',
'static_url_prefix':
'/statics/',
}
application = tornado.web.Application([
(
r"/index", IndexHandler),
], **settings)
if __name__ ==
"__main__":
application.listen(
8001)
tornado.ioloop.IOLoop.instance().start()
t1\app.py
==>
=====>
{
console.log(arg);
}
{
$.ajax({
url:
':8002/index',
type:
'POST',
data:{
'k1':
'v1'},
success:
function (arg) {
console.log(arg);
}
});
}
{
$.ajax({
url:
':8002/index',
dataType:
'jsonp',
jsonpCallBack:
'func'
})
}
>
index.html
tornado.ioloop
import tornado.web
:
:
self.write(
'func([11,22,33]);')
:
self.write(
't2.post')
settings={
'template_path':
'views',
'static_path':
'statics',
'static_url_prefix':
'/statics/',
}
application = tornado.web.Application([
(
r"/index", IndexHandler),
], **settings)
if __name__ ==
"__main__":
application.listen(
8002)
tornado.ioloop.IOLoop.instance().start()
t2\app.py
2、CORS
随着技术的发展,现在的浏览器可以支持主动设置从而允许跨域请求,即:跨域资源共享(CORS,Cross-Origin Resource Sharing),其本质是设置响应头,使得浏览器允许跨域请求。
* 简单请求 OR 非简单请求
条件:
1、请求方式:HEAD、GET、POST
2、请求头信息:
Accept
Accept-Language
Content-Language
Last-Event-ID
Content-Type 对应的值是以下三个中的任意一个
application/x-www-form-urlencoded
multipart/form-data
text/plain
注意:同时满足以上两个条件时,则是简单请求,否则为复杂请求
* 简单请求和非简单请求的区别?
简单请求:一次请求
非简单请求:两次请求,在发送数据之前会先发一次请求用于做“预检”,只有“预检”通过后才再发送一次请求用于数据传输。