跨域URL

Par*_*ash 8 javascript ajax

我试图在我的javascript代码中调用此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

这是我的javascript代码:

 $.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'); }
    });
Run Code Online (Sandbox Code Playgroud)

我在控制台中收到跨域URL错误.

有帮助吗?

Lui*_*uis 7

您需要使用JSONP进行跨站点请求调用尝试:

$.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);
    },
    });
Run Code Online (Sandbox Code Playgroud)

使用参数'jsonp'调用addressify服务将使服务在响应函数中包装响应,然后jquery ajax用于检索数据.所以$ .ajax参数'jsonpCallback'必须匹配你传递给服务'jsonp'的参数(在他们的文档中)

小提琴:

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