0

I have a good working local script:

$.getJSON("jsonfile.js",function(item) {    
  $.each(item.terra_nova_feed, function(i,item) {
    // functions and varibles//
  });
});

Which works fine local. When the JSON file moves to another domain.. nothing. I know I should be using getScript to pull in the JSON file. However how do I pull that data into the existing script?

1
  • You look like the guy from the killerphp videos :) Commented Jul 21, 2011 at 21:08

3 Answers 3

2

Try the $.ajax() function and use the crossDomain:true parameter. It essentially uses JSONP (padded JSON) which wraps the data in a callback.

$.ajax({
    url: "http://www.otherdomain.com/jsonfile.js",
    crossDomain:true,
    type:'get',
    dataType:'json',
    success: function(data) {    
        $.each(data.terra_nova_feed, function(i,data) {
            // functions and varibles//
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

crossDomain:true doesn't do anything in this case. The request is already cross-domain, so changing that option will have no effect because it defaults to true for crossdomain requests.
1

the short answer is you really can't do it. That being said, if you have control over what the other domain returns you can use something called JSON-P. With JSON-P you are basically calling a script file cross domain-- the script will need to be returned in such a way that it calls a method on your page. jQuery takes care of the plumbing on the calling page, but you still have to make sure the server with the script is emitting things correctly.

1 Comment

i see alot of answers involve setting the crossdomain to true, but I think that is only part of it. The script itself is going to have to be a function() call in order to execute correctly.
0

The problem is that $.getJson() doesn't usually work with jsonp (that means it doesn't work cross domain unless some special cases like an explicit support from the server passing it) but in your case i think you should use $.ajax() and set the crossDomain option to true.

1 Comment

Yes it does... as long as the webservice supports JSONP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.