Cross Domain URL

I am trying to call this url in my javascript code:

http://api.addressify.com.au/address/autoComplete?api_key=99acd24a-1c94-49ad-b5ef-6f90d0f126b1&term=1+George+st+t&state=nsw&max_results=5

This is my javascript code:

$.ajax({ url: 'http://api.addressify.com.au/address/autoComplete', type: 'GET', crossDomain: true, // enable this data: 'api_key=99acd24a-1c94-49ad-b5ef-6f90d0f126b1&term=1+George+st+t&state=nsw&max_results=5', // or $('#myform').serializeArray() success: function () { alert('PUT completed'); } }); 

I am getting a cross-domain url error in console.

Any help?

+8
javascript ajax
source share
2 answers

You need to use JSONP to invoke cross-site request requests:

 $.ajax({ url: 'http://api.addressify.com.au/address/autoComplete', type: 'GET', dataType:'jsonp', jsonpCallback:'callback', data: 'api_key=99acd24a-1c94-49ad-b5ef-6f90d0f126b1&term=1+George+st+t&state=nsw&max_results=5&jsonp=callback', // or success: function(json) { console.dir(json); }, }); 

Calling the addressing service with the jsonp parameter will force the service to wrap the response in a callback function, which then uses jquery ajax to retrieve the data. Therefore, the $ .ajax parameter 'jsonpCallback' must match the parameter you pass to the jsonp service (in the documentation )

Spell here:

http://jsfiddle.net/luisvsilva/cL1c3t4j/1/

+7
source share

Just add the data to the URL section of your ajax call.

 $.ajax({ url: 'http://api.addressify.com.au/address/autoComplete?api_key=99acd24a-1c94-49ad-b5ef-6f90d0f126b1&term=1+George+st+t&state=nsw&max_results=5', type: 'GET', crossDomain: true, // enable this success: function () { alert('PUT completed'); } }); 

If you cannot copy some of this data, you can turn the url declaration into a function.

 url: function() { return "http://api.addressify.com.au/address/autoComplete?api_key=" this.get("api_key") + "&term=" + this.get("term") + "&state=" + this.get("state") + "&max_results=" + this.get("max_results") } } 

I use the basic model methods to get the data - use everything you need.

+1
source share

All Articles