Lyrics


< More and better />


跨域

CORS

js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$.ajax({
url: 'http://localhost:3000/sign',
type: 'POST',
dataType :'JSON',
data:Obj,
xhrFields: {'Access-Control-Allow-Origin': '*'}, //CORS跨域指定 + dataType
success:function(data){
// alert("fff");
console.log(data)
},
error:function(err){
// console.log("err"+err);
}
});

node

1
2
3
4
5
6
7
8
9
10
11
12
13
//后端,服务器端
res.header("Access-Control-Allow-Origin", "*");
//跨域
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});

Jsonp

js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$.ajax({
url: 'http://localhost:3000/check',
type: 'GET',
cache: false,
dataType: 'jsonp',
data: { 'data': content.value },
jsonp: 'callback',
jsonpCallback: 'handler',
xhrFields: {
withCredentials: true
}, //设置发送cookie
crossDomain: true,
// success: handler,
error: function(jq, err, text) {
alert(err.message);
}
});