~~寫了段ajax 去請求接口數據的js ,無奈發現有跨域問題。javascript
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>index</title>html
</head>
<body>
<a id='ajax'>ajax</a>
</body>
</html>java
<script src="jquery-v1.10.2.js"type="text/javascript"></script>
<script type="text/javascript" >
$("#ajax").click(function(){
$.ajax({
async: false,
type: "GET",
dataType: 'json',jquery
url: "http://139.196.178.104/ops/v1.0/stuff",
data: "",
timeout: 3000,
contentType: "application/json;utf-8",
success: function(msg) {
console.log(msg);
}
});
});
</script>nginx
利用nginx反向代理ajax
1.建立虛擬域名json
在ajax.conf 中加一個locationapi
location ^~/api/{
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://139.196.178.104/;
}跨域
nginx -s reloadapp
這樣 訪問 http://local.ajaxdemo.com/api/ops/v1.0/stuff 地址 和訪問 http://139.196.178.104/ops/v1.0/stuff 獲得的數據效果是同樣 的說明 nginx 反向代理配置成功
而後修改ajax
$("#ajax").click(function(){
$.ajax({
async: false,
type: "GET",
dataType: 'json',
url: "api/ops/v1.0/stuff",
data: "",
timeout: 3000,
contentType: "application/json;utf-8",
success: function(msg) {
alert(msg);
console.log(msg);
}
});
});
即可實現抓取接口 http://139.196.178.104/ops/v1.0/stuff的數據