Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.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
Javascript 从服务器端到我的前端获取信息_Javascript_Node.js - Fatal编程技术网

Javascript 从服务器端到我的前端获取信息

Javascript 从服务器端到我的前端获取信息,javascript,node.js,Javascript,Node.js,我在后端node.js上使用,在前端使用常规的js。我试图通过以下线路从我的服务器发送信息: res.status(200).send({ message: "Hello!" }); 我在客户端尝试了一些代码。在下面的代码中,我没有得到消息 var request = new XMLHttpRequest(); request.onreadystatechange = function () { if (this.readyState == 4 && this.sta

我在后端node.js上使用,在前端使用常规的js。我试图通过以下线路从我的服务器发送信息:

res.status(200).send({ message: "Hello!" });
我在客户端尝试了一些代码。在下面的代码中,我没有得到消息

var request = new XMLHttpRequest();

request.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        console.log(request);
    }
};

request.open('GET', 'http://localhost:3000/newUser');
request.send();
这是我在控制台中得到的:

XMLHttpRequest{readyState:4,超时:0,withCredentials:false,upload:XMLHttpRequestUpload,onreadystatechange:ƒ,…} onabort:null onerror:null onload:null onloadend:null onloadstart:null onprogress:null onreadystatechange:ƒ() 参数:null 呼叫方:空 长度:0 姓名:“ 原型:{constructor:ƒ} proto:ƒ() [[FunctionLocation]]:newUser.js:111 [[Scopes]]:作用域[2] ontimeout:null 重新启用日期:4 答复:“ 响应文本:“ 响应类型:“ 答:“” responseXML:null 现状:200 状态文本:“确定” 超时:0 upload:XMLHttpRequestUpload{onloadstart:null,onprogress:null,onabort:null,onerror:null,download:>null,…} withCredentials:false 协议:XMLHttpRequest

我也尝试过使用POST而不是GET,但是我没有从控制台得到任何东西。 有没有其他方法可以将信息从服务器传送到我的客户端

试试这个例子

fetch('http://dummy.restapiexample.com/api/v1/employees',{
    method:'GET'
}).then(response => response.json()).then((value)=>{
    console.log(value)
}).catch(error=>{
    console.log(error);
})
试试这个例子

fetch('http://dummy.restapiexample.com/api/v1/employees',{
    method:'GET'
}).then(response => response.json()).then((value)=>{
    console.log(value)
}).catch(error=>{
    console.log(error);
})

使用而不是直接使用XMLHttpRequest@v.kostenko我想只在客户端使用fetch吗?是的,在服务器端没有原生的
fetch
use,而不是直接使用XMLHttpRequest@v.kostenko我想只在客户端使用fetch吗?是的,在服务器端没有原生的
fetch