admin管理员组文章数量:1332377
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/test.jsp
i.e. something like this
var data = get("www.test/test.jsp");
var jsonObj = JSON.parse(data);
etc..
Is that possible?
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./test.jsp
i.e. something like this
var data = get("www.test./test.jsp");
var jsonObj = JSON.parse(data);
etc..
Is that possible?
Share Improve this question edited Apr 9, 2013 at 17:54 Trevor asked Apr 9, 2013 at 15:57 TrevorTrevor 16.1k9 gold badges55 silver badges85 bronze badges 5- What is an "end point"? – Pointy Commented Apr 9, 2013 at 15:58
- Lets just say its a URL to a .jsp file containing the data. – Trevor Commented Apr 9, 2013 at 15:58
- Is your question how to do this in java or javascript (can't tell based on your tags) – Gordon Tucker Commented Apr 9, 2013 at 16:03
- Javascript would be preferred. – Trevor Commented Apr 9, 2013 at 16:07
- 1 possible duplicate of I have a nested data structure / JSON, how can I access a specific value? – Felix Kling Commented Apr 9, 2013 at 16:44
4 Answers
Reset to default 3I 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
jQuery makes this simple:
<html>
<head>
<script src="//ajax.googleapis./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>
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.
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.
本文标签: javaHow to GET data from end point using JavaScriptStack Overflow
版权声明:本文标题:java - How to GET data from end point using JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742305947a2449919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论