Cross Domain URL

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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 error of Cross Domain URL in console.

Any help?

回答1:

You need to use JSONP to make cross site request calls try this:

$.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 addressify service with the parameter 'jsonp' will make the service wrap the response in a callback function, which then jquery ajax uses to retrieve the data. So the $.ajax parameter 'jsonpCallback' must match the parameter you pass to the service 'jsonp' (in their documentation)

Fiddle here:

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



回答2:

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 can't hard code 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'm using the backbone model methods for getting the data - use whatever you need to do.



文章来源: Cross Domain URL
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!