0

How can I use public APIs in a Javascript application? For example I want to make a call to the Zillow API using JQuery AJAX. When issuing the request in JQuery AJAX (shown below) I get the following error: XMLHttpRequest cannot load "MY HTTP REQUEST URL". Origin "MY WEB DOMAIN" is not allowed by Access-Control-Allow-Origin.

var requesturl = "http://www.zillow.com/webservice/GetRegionChildren.htm?zws-id="+zwsid+"&state="+state+"&city="+city+"&childtype=neighborhood";

Code:

var jqxhr = $.ajax({  
url: requesturl
})
.done(function(data) { 
    console.log(data);
});

I've also tried adding crossDomain, dataType and headers params (shown below), but they haven't helped.

var jqxhr = $.ajax({  
url: requesturl,
crossDomain: true,
dataType: 'xml',
headers: { 'Access-Control-Allow-Origin': '*' },
beforeSend: setHeader
})
.done(function(data) { 
    console.log(data);
});

2 Answers2

0

Most popular public API's support JSONP request. Refer API documentation for details.

Cross ajax domain request is restricted. So you will be needing to make JSONP request. Don't worry JQuery will handle most of it.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

Sounds like you need to register your url with Zillow, maybe contact them / hunt around on their doc pages. Also jquery has a get method which makes ajax requests even simpler. There's also getJSON if that is the return format.

Iain_b
  • 1,043
  • 5
  • 13