標籤:ajax跨域 ica proxy break 虛擬 query com utf-8 java
~~寫了段ajax 去請求介面資料的js ,無奈發現有跨域問題。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>index</title>
</head>
<body>
<a id=‘ajax‘>ajax</a>
</body>
</html>
<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‘,
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反向 Proxy
1.建立虛擬網域名稱
在ajax.conf 中加一個location
location ^~/api/{
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://139.196.178.104/;
}
nginx -s reload
這樣 訪問 http://local.ajaxdemo.com/api/ops/v1.0/stuff 地址 和訪問 http://139.196.178.104/ops/v1.0/stuff 得到的資料效果是一樣 的說明 nginx 反向 Proxy配置成功
然後修改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的資料
nginx 反向 Proxy解決ajax跨域問題