使用 Javascript 的 Json 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4608517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json request with Javascript
提问by sam_33
I am just beginner on java script and JSON and never had done any work in these before. My employer has just asked me to create the basic POC of these.
Scenario:I have a REST API and when i call it, it returns back response in JSON format.
Need to To:Create a HTML page and use javascript to call that REST API and capture JSON response and print in the same HTML page.
我只是 Java 脚本和 JSON 的初学者,以前从未在这些方面做过任何工作。我的雇主刚刚要求我创建这些的基本 POC。
场景:我有一个 REST API,当我调用它时,它以 JSON 格式返回响应。
需要:创建一个 HTML 页面并使用 javascript 调用该 REST API 并捕获 JSON 响应并在同一 HTML 页面中打印。
<script type="text/javascript">
function loadMe() {
loadJSON('http://myrestAPI');
}
function loadJSON(url) {
//Help me here to capture the response and print in html page.
}
</script>
I would appreciate your help. This might be simple, but for me i have no idea because i never have done anything similar in java script and json. I goggled but could not find anything.
我会很感激你的帮助。这可能很简单,但对我来说我不知道,因为我从未在 java 脚本和 json 中做过类似的事情。我目瞪口呆,但找不到任何东西。
Thanks, chota
谢谢,乔塔
回答by Praneeth
function SendRequest(MSG)
{
var objJSON = {
"msg" : MSG
};
var strJSON = encodeURIComponent(JSON.stringify(objJSON));
new Ajax.Request("ReceiveJSON.jsp",
{
method: "post",
parameters: "strJSON=" + strJSON,
onComplete: Respond
});
}
function Respond(REQ)
{
document.getElementById("ResponseDiv").innerHTML=REQ.responseText;
}
回答by Harish
If you view your REST request response, the JSON might be something like this
如果您查看 REST 请求响应,JSON 可能是这样的
{
"users": [
{"first": "Peter",
"last": "Griffin"}
],
"books": [
{"title": "Design Patterns",
"author": "The Gang of Four",
"year": 1995}
]
}
This rest query result can be loaded as a javascript array object
这个rest查询结果可以作为javascript数组对象加载
<script language="javascript">
var resultObj = <Result of your REST url + query pasted here>;
</script>
Then,
然后,
you access it in your html with javascript blocks like
你在你的 html 中使用 javascript 块访问它
document.write(resultObj.users[0].first)
which would return 'Peter'
哪个会返回 'Peter'
回答by JohnO
Praneeth has the simple javascript answer. If you can use jquery (and there really is no reason you cannot) it gets way easier:
Praneeth 有简单的 javascript 答案。如果您可以使用 jquery(并且确实没有理由不能),它会变得更容易:
<div id="response"></div>
$.get(url, function(data) {
$('#response').append(data.property);
});
回答by the_new_mr
Here's a complete working example using jQuery that should work.
这是一个使用 jQuery 的完整工作示例,应该可以工作。
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.0.min.js"></script>
</head>
<body>
<div id="response">
<p id="responseParagraph">Base text</p>
</div>
<script>
$.getJSON('http://myrestAPI',
function(data) {
$('#responseParagraph').append("<p>"+data.responseMessage+"</p>");
});
</script>
</body>
</html>
回答by Praneeth
$.getJSON(url, null, function (data) { .... });