繁体   English   中英

如何使用javascript查看快递发送的json数据?

[英]How to view json data sent from express using javascript?

使用一个节点,我有一个client-server model ,客户端将连接到“localhost”,并且 express 将提供基于逻辑的response 我了解如何通过response发送 json object,但我不知道如何在我的客户端脚本中访问此 json ZA8CFDE6331BD59EB266AC96F8 新手有什么想法吗?

服务器:

app.get("/", function(request,response){
 response.json({ username: 'example' })
 
 })

如果您使用 Vanilla JS,您有几个选项,例如 Fetch API、XMLHttpRequest、ajax。

1.获取API:

fetch('http://localhost:3000/')
    .then(response => console.log(response));

2. XMLHttpRequest:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       console.log(xhttp.responseText);
    }
};
xhttp.open("GET", "http://localhost:3000/", true);
xhttp.send();

3. Ajax:

$.get("http://localhost:3000/", function(data, status){
    console.log(data);
  });

您可以用您的 URL 替换"http://localhost:3000/"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM