all, I tried to use a service from https://developers.auspost.com.au/apis/pac/reference/postcode-search , and it requires the auth-key in the header. Hence, I tried to test it on Advanced Rest Client on chrome, it works! But when I tried to call this service by ajax, I got denied as the error message says I don't have the access to this service.
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);
}
});
});
And I also tried:
beforeSend: function (xhr) { xhr.setRequestHeader("auth-key", "c12a9e06-****-443e-bb2d-5220c70f3ac9"}
instead of headers:{}
However, it is still not working.
JSFiddle:
http://jsfiddle.net/matildayipan/yxrfabgL/
Can anyone please look into that?
Thank you very very very much .
[UPDATES:]
When I looked into the network, the type of of request become "OPTIONS", why is that?
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
Apparently, the auth-key didn't really send out since it is not appear in the network details.
So I tried to add : `headers:{ "auth-key": "c12a9e06-1a75-443e-bb2d-5220c70f3ac9", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET", "Access-Control-Allow-Headers": "Authorization" },` Here is the updated JSFiddle: http://jsfiddle.net/matildayipan/yxrfabgL/14/ – Matilda Yi Pan Nov 14 '14 at 03:07