-4

Can anyone tell me what is wrong with this ?

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {
    var url = 'http://www.google.com/ig/calculator?hl=en&q=100INR=?USD';
    var title = "jQuery";

    $.getJSON("http://www.google.com/ig/calculator?hl=en&q=100INR=?USD" + "&format=json&callback=?", function(data) {
        alert(data);
    });
});

<div id="div1">Test Page</div>

I am making the call and I am getting an error. You know why ? How do I make ajav calls to the url http://www.google.com/ig/calculator?hl=en&q=100INR=?USD

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Anirban
  • 589
  • 3
  • 16
  • 40
  • 5
    What is your error message ? – Harsha Venkataramu Sep 03 '13 at 21:01
  • That error you get...that is in the info we need to help! (I'm going to guess SOP tho) – tymeJV Sep 03 '13 at 21:01
  • 1
    The URL returns JSON, not JSONP. But you are telling jQuery to expect JSONP by using `callback=?`. As a result the response will be evaluated as JavaScript (that's what JSONP is), but JSON is not a valid JavaScript script, and therefore you get an error. – Felix Kling Sep 03 '13 at 21:04
  • 1
    Duplicate of: http://stackoverflow.com/questions/1586003/jquery-getjson-url-data-callback – keithhatfield Sep 03 '13 at 21:07

1 Answers1

0

You can't make AJAX calls cross-domain. You will need to use something like JSONP to make it work cross-domain.

jQuery's $.ajax method supports JSONP requests. If the Google API doesn't support JSONP style responses, you are out of luck. Using a proxy, as suggest by @FelixKling, is the other option.

zigdawgydawg
  • 2,046
  • 13
  • 10
  • 2
    When adding `callback=?` to the URL, `$.getJSON` will make a "JSONP request" too. The problem is that the URL does not return JSONP. You cannot just "use" JSONP, the server side has to support it. – Felix Kling Sep 03 '13 at 21:07
  • @Anirban: Did you read my comment? You *are already* trying to use JSONP, but since JSONP is not supported by the API, you cannot use it. The only solution is to proxy the request through your server or a CORS enabled external proxy. – Felix Kling Sep 03 '13 at 21:09
  • @FelixKling - Yes...you are correct. Unless the API supports JSONP responses, you are out of luck (without going through a proxy). – zigdawgydawg Sep 03 '13 at 21:38