Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json请求与Javascript_Javascript_Json_Response - Fatal编程技术网

Json请求与Javascript

Json请求与Javascript,javascript,json,response,Javascript,Json,Response,我只是java脚本和JSON的初学者,以前从未在这方面做过任何工作。我的雇主刚刚要求我创建这些产品的基本POC。 情景: 我有一个RESTAPI,当我调用它时,它以JSON格式返回响应。 需要: 创建一个HTML页面并使用javascript调用RESTAPI,捕获JSON响应并在同一HTML页面中打印 <script type="text/javascript"> function loadMe() { loadJSON('http://myrestAPI')

我只是java脚本和JSON的初学者,以前从未在这方面做过任何工作。我的雇主刚刚要求我创建这些产品的基本POC。
情景: 我有一个RESTAPI,当我调用它时,它以JSON格式返回响应。
需要: 创建一个HTML页面并使用javascript调用RESTAPI,捕获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>

函数loadMe(){
loadJSON('http://myrestAPI');
}
函数loadJSON(url){
//在这里帮助我捕获响应并在html页面中打印。
}
谢谢你的帮助。这可能很简单,但对我来说我不知道,因为我从来没有在java脚本和json中做过类似的事情。我目瞪口呆,但什么也找不到

谢谢,
chota

如果查看REST请求响应,JSON可能是这样的

{
    "users": [
        {"first": "Peter",
         "last": "Griffin"}
    ],
    "books": [
        {"title": "Design Patterns",
         "author": "The Gang of Four",
         "year": 1995}

    ]

}
此rest查询结果可以作为javascript数组对象加载

<script language="javascript">

var resultObj = <Result of your REST url + query pasted here>;

</script>

它将返回
'Peter'

Praneeth有一个简单的javascript答案。如果您可以使用jquery(实际上没有理由不能),它会变得更容易:

<div id="response"></div>
$.get(url, function(data) {
  $('#response').append(data.property);
});

$.get(url、函数(数据){
$('#response').append(data.property);
});

下面是一个使用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>

基本文本

$.getJSON('http://myrestAPI', 功能(数据){ $(“#responseParagraph”).append(“”+data.responseMessage+”

”); });
不,我不能。jQuery就是javascript:P有什么理由不能使用它吗?我想知道是否有一位雇主明确告诉我,我不能使用让我工作效率更高的操作系统包(当然法律原因除外)…jQuery是javascript…你确定吗?jQuery非常庞大,有时你需要在没有它的情况下开发。这种情况的一个很好的例子是用户脚本,或者任何您只需要做一件事的情况。也就是说,我几乎总是使用jQuery。jQuery的容量高达26kB。即使是谷歌的简单主页也有260+kB,我会说“非常庞大”有点夸张。这对我来说理解起来非常复杂。你能解释一下你不能理解的部分吗?Jax对象在我的浏览器中不存在。将数据附加到div是没有意义的。你可以在div中有一个元素,然后再附加到它。我的错。这是有道理的,也是有效的。我没想清楚。很抱歉
document.write(resultObj.users[0].first)
<div id="response"></div>
$.get(url, function(data) {
  $('#response').append(data.property);
});
<!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>