全部 我尝试使用https://developers.auspost.com.au/apis/pac/reference/postcode-search中的服务 ,它需要标头中的auth-key。因此,我尝试在Chrome上使用Advanced Rest Client进行测试,它可以正常工作! 但当我试图通过ajax调用此服务时,我被拒绝,因为错误消息显示我无法访问此服务。
JS:
$(document).ready(function(){
var result;
$.ajax({
url:"https://auspost.com.au/api/postcode/search.json",
type:"GET",
headers: {
"auth-key": "c12a9e06-****-443e-bb2d-5220c70f****"
},
data: {
"q": "Meadow",
"state": "NSW"
},
async: false,
success: function(response){
result = jQuery.parseJSON(response);
$("#resp").html(result);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
response = "err--" + XMLHttpRequest.status + " -- " + XMLHttpRequest.statusText;
result = response;
$("#resp").html(result);
}
});
});
我也尝试过:
beforeSend: function (xhr) { xhr.setRequestHeader("auth-key", "c12a9e06-****-443e-bb2d-5220c70f3ac9"}
而不是headers:{}
但是,它仍然无法正常工作。
JSFiddle:
http://jsfiddle.net/matildayipan/yxrfabgL/
任何人都可以调查一下吗?
非常感谢你。
[更新:
当我查看网络时,请求的类型变为“OPTIONS”,为什么会这样?
Request URL:https://auspost.com.au/api/postcode/search.json?q=Meadow&state=NSW
Request Method:OPTIONS
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2
Access-Control-Request-Headers:accept, auth-key
Access-Control-Request-Method:GET
Connection:keep-alive
Host:auspost.com.au
Origin:http://fiddle.jshell.net
Referer:http://fiddle.jshell.net/_display/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome / 38.0.2125.111 Safari / 537.36
显然,auth-key并没有真正发送出去,因为它没有出现在网络细节中。