1

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.

Matilda Yi Pan
  • 658
  • 2
  • 8
  • 26
  • Have you tried to add `dataType: "jsonp"` option to the ajax call? – ekad Nov 14 '14 at 01:43
  • @ekad I tried, if I added `dataType: "jsonp" `, the whole thing stop working completely. – Matilda Yi Pan Nov 14 '14 at 02:51
  • Try to add in headers : {'Content-Type': 'application/json' , "auth-key": "c12a9e06-1a75-443e-bb2d-5220c70f3ac9" } – Neha Nov 14 '14 at 02:52
  • @Neha, I tried, it doesn't work. I got error message as : XMLHttpRequest cannot load https://auspost.com.au/api/postcode/search.json?q=Meadow&state=NSW. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access. The response had HTTP status code 500. – Matilda Yi Pan Nov 14 '14 at 02:55
  • Are u trying to access cross domain from ajax? its not allowed. – Neha Nov 14 '14 at 02:58
  • @Neha , It is allowed, please refer to :http://stackoverflow.com/questions/9559947/cross-origin-authorization-header-with-jquery-ajax
    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

0 Answers0