簡體   English   中英

從服務器端獲取信息到我的前端

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

我在我的后端 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, 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

我也嘗試使用 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);
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM