title: Nginx直接返回值给ajax调用
urlname: 10_Nginx直接返回值给ajax调用
date: 2017-04-29 03:03:03
tags: [nginx]
categories: 网络安全
Nginx直接返回值给ajax调用的方法
js调用代码 #:
function getDomain(){
var url="http://47.81.12.22/get_t.do";
$.ajax({
type : "get",
url : url,
async: false,
dataType: "text",
success : function callback(result) {
mydoain=result;
},
error:function(){
mydoain="http://www.xxx.com/";
}
});
alert(mydoain);
}
nginx服务器配置代码
location = '/get_t.do' {
default_type text/html;
add_header Access-Control-Allow-Origin *;
return 200 'http://www.test.com/';
}
add_header Access-Control-Allow-Origin *;
这一句很重要,不加的话取js不到结果哦.
也可以 add_header Access-Control-Allow-Origin http://xxoo.com; --指定允许的url;
这里面可以加很多头部信息
nginx中配置跨域支持功能
在nginx.conf中配置:
http {
......
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
......
}
这样就可以实现GET,POST,OPTIONS的跨域请求的支持
也可以 add_header Access-Control-Allow-Origin http://test.51testing.com; --指定允许的url;
配置项详细解释:w3c-Cross-Origin Resource Sharing
QQ群:397745473