0

Lets say there is a url out there e.g. www.website.com/data.jsp

the link has the following JSON data

{"successful":"true","rows":[{"zip":"65472","user_id":"10843","name":"Rufio"}]}

I just want to be able to extract this data at runtime however I am having a hard time getting it using getJSON

$.getJSON("test2.jsp",function(result){
                $("div").append(result.rows[0].user_id + " ");
            });

Now if I run it using a local file with the data residing in test2.jsp as shown above it appends the user_id. However when I try to access "www.website.com/data.jsp" instead nothing happens. I don't believe the website is configured to work with JSONP either.

I need a way to figure out how to pull this data from the website at run time. Does anyone have any solutions or workarounds?

p.s. Is this something that might need to be sorted out on the other end? The people who own the website set this scenario up to be like a fake api call like typically you would pass in parameters to get back the specific information that you would need. In the case of this endpoint or url it just returns a single record or the file just contains the data listed above. They would like me to extract the data from their url at runtime.

Trevor
  • 16,080
  • 9
  • 52
  • 83
  • Try http://stackoverflow.com/questions/2681466/jsonp-with-jquery – anazimok Apr 10 '13 at 01:30
  • Doesn't that mean that the server has to be set up to work with jsonp? Or would that example work even if the server I'm trying to get the data from is not setup for jsonp? – Trevor Apr 10 '13 at 01:32
  • I dont think the server has to be setup to use jsonp, its just the data method in your ajax – Jay Rizzi Apr 10 '13 at 01:33
  • 1
    Read up about cross-origin resource sharing. JSONP is a workaround to achieve this. Or you could use the CORS header. Whatever it is, to do it from the client, you need the target website to modify their system. – Chris Morgan Apr 10 '13 at 01:34
  • Yes I have to do it from the client.. So I need to contact the target website and ask them to setup their page to work with JSONP or CORS header? – Trevor Apr 10 '13 at 01:36
  • 1
    this post might help! http://www.jquery4u.com/ajax/jquery-php-ajax-json/ – Sam Deering Apr 10 '13 at 01:39
  • Thank you, i'll look into your example and give it a try! In the meantime I just want to verify so its impossible to do this using soley Jquery JSON/JSONP unless the target website is setup to accept JSONP right? – Trevor Apr 10 '13 at 01:43

1 Answers1

2

You can't make a normal ajax call to to this other domain due to same origin policy.

You can use JSONP to load the remote page, but looking at that example output you wouldn't be able to access the data unless the remote site is setup for JSONP (assigning the JSON to a variable, calling a callback function, etc).

You could create a server-side passthrough script of your own. You don't mention what server-side technology you have available, but if you can use PHP, you do a passthrough like this:

<?php
echo file_get_contents("http://www.website.com/data.jsp");
?>

PHP (or any other server-side language) can fetch the remote data, and now you can use ajax to call your own script (which works since you're on the same domain).

  • Thank you this worked great. What I ended up doing when I got the data was created a new file with the data in it. And then did a getJson from there. Not sure if this is the most efficient way to do it or if there is another way. Do yo have any thoughts on it? Thanks – Trevor Apr 10 '13 at 04:10
  • 1
    $myFile = "test.jsp"; $stringData = file_get_contents($_GET["site"]); file_put_contents($myFile,$stringData); –  Apr 10 '13 at 12:07