Tim
Tim

Reputation: 21

getJSON remote request without JSONP - Server

I am trying to call JSON-data from a remote Server, but the server doesnt return valid JSONP formed data, only valid JSON data. ( confirmed with validators )

If i make the getJSON request( with &callback=? ), i get the valid JSON returned, but it doesnt trigger the callback function because it isnt valid JSONP.

Is there any good way to get access to the JSON data that was returned?

Upvotes: 2

Views: 3406

Answers (3)

Cheng Chen
Cheng Chen

Reputation: 113

Yea, it sucks. I'm having the same issue with the Viddler API (browser side). It's sending JSON data back, but not in scripted JSON-P interface.

Thus, you have two options:

  1. As people have said, use a back-end proxy to make requests server side.
  2. If you only need to support one browser (Chromium in my case), use something like "chromium-browser --disable-web-security" to disable the "same-origin-policy" rule. This will allow you to make cross-domain requests, but only for this very specific situation.

Good luck man!

Upvotes: 2

Matt
Matt

Reputation: 1265

Well first of all if your server your getting the JSON from doesn't support JSONP then your going to have to use a proxy. If it does support JSONP you should format your request a little like the sample below. When you don't specify a callback in the $.getJSON(...&callback=?) then your request looks like this:

http://someurl?callback=123489234982

with some ridiculous number on the end of it and it makes your life a pain. So, you should specify a callback and format your code like the following:

<script>
     $.getJSON("your url?callback=callbackName", ....);

     function callbackName { do what you want with the json in here }
</script>

if that doesn't work then your server doesn't support JSONP :( here is a link to a php proxy you could use that is pretty good and has lots of documentation.

http://benalman.com/projects/php-simple-proxy/

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630449

Nope, it has to be JSONP data because of how the whole thing works, it's basically including a JavaScript file by creating a <script> tag...and that response has to be valid JavaScript, an object literal (by itself) isn't valid JavaScript.

Think of it another way: if we could get JSON from a remote server, why would JSONP exist? :)

Upvotes: 2

Related Questions