0

(extremely ignorant question, I freely admit)

I have a simple web page with a button and a label. When I click the button, I want to make a REST call to an entirely different domain (cross-domain, I know that much) and display the results (HTML) in the label.

With other APIs, I've played around with using JSON/P and adding a element on the fly, but this particular API doesn't support JSON so I'm not sure how to go about successfully getting through.

The code I have is:

    function getESVData() {
    $.get('http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=John+1', function (data) {
        $('#bibleText').html(data);
        app.showNotification("Note:", "Load performed.");
    });
}

I get an "Access denied." Is there anyway to make this call successfully without JSON?

  • In this case you can't do it with AJAX (directly) because they does not implement CORS. So you will have to write proxy script, host it on your server and make AJAX requests to it. – dfsq Mar 30 '13 at 07:48

1 Answers1

6

First off, JSON and JSONP are not the same. JSON is a way of representing information, and JSONP is a hack around the same-origin policy. JSONP works by requesting information from another domain, and that domain returns a script which calls a function (with the name you provided) with the information. You are indeed executing a script on your site that another domain gave to you, so you should trust this other domain.

Now when trying to make cross domain requests you basically have 3 options:

  1. Use JSONP. This has limitations, including the fact that it only works for GET requests, and the server you are sending the request to has to support it.
  2. Make a Cross Origin Resource Sharing (CORS) request. This also must be supported by the server you are sending the request to.
  3. Set up a proxy on your own server. In this situation you set an endpoint on your site that simply relays requests. ie you request the information from your server, your server gets it from the other server and returns it to you.

For your situation, it the other server doesn't have support for other options, it seems like you will have to go with options 3.

Nick Mitchinson
  • 5,452
  • 1
  • 25
  • 31