[英]I'm trying to send JavaScript data to my NodeJS server
所以我試圖通過 POST 請求將地理定位數據發送到 NodeJS,但是當我在我的 NodeJS 代碼中控制台記錄數據時,它只顯示一個空對象。
我已經用郵遞員測試過它,我可以毫無問題地接收數據。 我認為問題出在我的應用程序的客戶端
//**This is in the client Side(pure JS);
async function getWeather(position){
let coords ={
lat: position.coords.latitude,
long: position.coords.longitude
}
const options = {
method: "POST",
body: JSON.stringify(coords),
headers: {
"Content-Type": "aplication/json"
}
};
let response = await fetch("http://localhost:3000/weather", options);
let location = await response.json();
}
.
//**This is in the server side
app.post('/weather',(req,res)=>{
let coords = req.body;
console.log(coords); //This shows an empty object
res.sendStatus(200);
});
在您的客戶端,您可以嘗試此代碼。 您可以將 url 替換為您的端點並嘗試獲取結果。 如下所示,我在執行腳本后將響應返回到data
。
我希望這有幫助。
(async () => { const rawResponse = await fetch('https://httpbin.org/post', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({lat: 10, long: 20}) }); const content = await rawResponse.json(); console.log(content); })();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.