admin管理员组文章数量:1391947
I am trying to call this URL in my javascript code:
;term=1+George+st+t&state=nsw&max_results=5
This is my javascript code:
$.ajax({
url: '',
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 pleted'); }
});
I am getting error of Cross Domain URL in console.
Any help?
I am trying to call this URL in my javascript code:
http://api.addressify..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..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 pleted'); }
});
I am getting error of Cross Domain URL in console.
Any help?
Share Improve this question edited Sep 7, 2015 at 5:39 Parkash asked May 12, 2015 at 2:01 ParkashParkash 1051 silver badge12 bronze badges 2- 3 For a cross domain Ajax request to work, the server must support cross domain access either with CORS or with JSONP. – jfriend00 Commented May 12, 2015 at 2:09
- possible duplicate of XMLHttpRequest cannot load an URL with jQuery – Nizam Commented May 12, 2015 at 2:25
2 Answers
Reset to default 7You need to use JSONP to make cross site request calls try this:
$.ajax({
url: 'http://api.addressify..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/luisvsilva/cL1c3t4j/1/
Just add the data to the URL section of your ajax call.
$.ajax({
url: 'http://api.addressify..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 pleted'); }
});
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..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.
本文标签: javascriptCross Domain URLStack Overflow
版权声明:本文标题:javascript - Cross Domain URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744687096a2619768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论