1

There is some data stored at a end point(url). And the file is a .jsp file.

The following is the data.

{"successful":"true","rows":[{"zip":"56431","user_id":"35","name":"test"}]}

How can I get the data from this end point? www.test.com/test.jsp

i.e. something like this

var data = get("www.test.com/test.jsp"); 
var jsonObj = JSON.parse(data);

etc..

Is that possible?

Trevor
  • 16,080
  • 9
  • 52
  • 83

4 Answers4

3

I hope this code can help you,

var data = '{"successful":"true","rows":[{"zip":"56431","user_id":"35","name":"test"}]}'
var jsonObj = JSON.parse(data);
var userID = jsonObj.rows[0].user_id;

var name = jsonObj.rows[0].name
Rakesh Chouhan
  • 1,196
  • 12
  • 28
1
var obj = JSON.parse(string);//Were string is your data string

See this answer for more details.

I don't know what you mean by end point, but I hope you aren't parsing the data from the URL.

Community
  • 1
  • 1
Hugo Tunius
  • 2,869
  • 24
  • 32
1

jQuery makes this simple:

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function callEndpoint() {
    $.getJSON('/endpoint.jsp', function(data) {
        $('#output').append(data.rows[0].user_id);
    });
}
callEndpoint();
</script>
<body>
<div id="output"></div>
</body>
</html>
laz
  • 28,320
  • 5
  • 53
  • 50
  • This looks great however for some reason it keeps freezing up in the callEndpoint function.. I have been running this code in phpstorm and trying a number of things such as accessing a local .jsp file as opposed to an external one.. I even tried running an w3shools example locally on my computer and it seemed to freeze after the $.getJSON call. meaning if I put an alert message after it it never gets to it.. Any ideas what might be causing this issue? – Trevor Apr 09 '13 at 19:39
  • 1
    It may be silently failing. I recommend using Firebug to step through the code. The `getJSON` function also allows you to access more details around the success or failure of the AJAX request. Look at http://api.jquery.com/jQuery.getJSON/ – laz Apr 09 '13 at 19:45
  • For some reason when running the chrome browser via phpStorm it was not working for me. Ironically I installed FireFox so that I could run Firebug, I was planning on doing some debugging when it worked.. So apparently PHPStorm-Chrome->$.getJson (mac) combo was not working for me.. But firefox did the trick. – Trevor Apr 09 '13 at 21:53
1

If you are looking for an AJAX solution, you can use this function. It can send variables through the URL and receive a response from the source url.

function get_(url, func)
{
 var http;
 try { http = new XMLHttpRequest(); } catch (e) { try { http = new ActiveXObject(\"Msxml2.XMLHTTP\"); } catch (e) { try { http = new ActiveXObject(\"Microsoft.XMLHTTP\"); } catch (e) { alert(\"Your browser broke!\"); return false; } } }

 http.open(\"GET\", url, true);
 http.onreadystatechange = function() { if(http.readyState == 4) { func(http); } }
 http.send(null);
}

To use this, here is an example of how a button triggers the call and specified a response handler function:

HTML

<button onClick='get_("source_url.jsp", showResponse);'> Show the response </button>

JAVASCRIPT

function showResponse(h) { alert(h.responseText); }

To be clear, the second parameter of the get_ function is a reference to a function. Whatever function you specify when using the get_ function, it passes a single parameter which containes the .responseText property, which is the output from the source_url file.

I use this function all the time, though I have another version that works with my PHP server to authenticate the user so that there is no unauthorized loading/saving of information from/to the server.

  • Thanks for the input jack, unfortunately I am not familiar with Ajax so I don't know how I would implement the get_ function.. An quick example with the full source would be helpful if its not too much trouble. – Trevor Apr 09 '13 at 19:34
  • 1
    Actually, the code is complete as-is. The get_ function is a normal javascript function. It's counterpart is the showResponse javascript function. All you need to do is replace "source_url.jsp" with the actual file name that outputs the text you want your page to receive. For the sake of an example, it could even be a static html file. If it was php, anything that get printed or echoed will be shown in the alert dialog. – Jack Thomson Apr 10 '13 at 07:33