简体   繁体   English

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

[英]Get info from server-side to my front-end side

I am using on my back-end node.js and in my front-end a regular js.我在我的后端 node.js 和我的前端使用常规 js。 I was trying to send info from my server by this line:我试图通过这一行从我的服务器发送信息:

res.status(200).send({ message: "Hello!" });

I tried a few codes in my client-side.我在客户端尝试了一些代码。 in the code below I didn't get the message.在下面的代码中,我没有收到消息。

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();

this is what I got in my console:这是我在控制台中得到的:

XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …} onabort: null onerror: null onload: null onloadend: null onloadstart: null onprogress: null onreadystatechange: ƒ () arguments: null caller: null length: 0 name: "" prototype: {constructor: ƒ} proto : ƒ () [[FunctionLocation]]: newUser.js:111 [[Scopes]]: Scopes[2] ontimeout: null readyState: 4 response: " responseText: " responseType: "" responseURL: " http://localhost:3000/newUser " responseXML: null status: 200 statusText: "OK" timeout: 0 upload: XMLHttpRequestUpload {onloadstart: Z37A6259CC0C1D XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …} onabort: null onerror: null onload: null onloadend: null onloadstart: null onprogress: null onreadystatechange: ƒ () arguments: null调用者:null 长度:0 名称:“”原型:{构造函数:ƒ}原型:ƒ () [[FunctionLocation]]: newUser.js:111 [[Scopes]]: Scopes[2] ontimeout: null readyState: : " responseText: " responseType: "" responseURL: " http://localhost:3000/newUser " responseXML: null 状态: 200 statusText: "OK" 超时: 0 上传: XMLHttpRequestUpload {0CCCstart: Z37A629 AE299A7866489DFF0BDZ, onprogress: null, onabort: null, onerror: null, download: > null, …} withCredentials: false proto : XMLHttpRequest AE299A7866489DFF0BDZ, onprogress: null, onabort: null, onerror: null, download: > null, …} withCredentials: false proto : XMLHttpRequest

I have also tried to use POST instead of GET but I didn't get anything from the console.我也尝试使用 POST 而不是 GET,但我没有从控制台得到任何东西。 Is there any other way to get the massage from the server to my client?有没有其他方法可以从服务器获取按摩到我的客户?

Try with this example试试这个例子

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

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

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