获取 post 正文数据为空
Fetch post with body data coming as empty
我正在客户端获取我的当前位置并且我正在使用 fetch。
if ('geolocation' in navigator) {
console.log('geolocation available');
navigator.geolocation.getCurrentPosition(async (position) => {
console.log(position.coords.latitude, position.coords.longitude);
let lat = position.coords.latitude;
let lng = position.coords.longitude;
document.getElementById('latitude').textContent = lat;
document.getElementById('longitude').textContent = lng;
const data = { lat, lng }
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify(data)
};
console.log(options, "options")
let response = await fetch('/api', options)
const datas = await response.json();
console.log(datas, 'after fetch');
});
} else {
console.log('geolocation not available')
}
在服务器端我正在做 post
const express = require("express");
const app = express();
app.use(express.static('public'));
app.use(express.json({ limit: '1mb' }));
// app.use(express.urlencoded());
app.listen(3000, () => console.log('listening at 3000'))
app.post('/api', (request, response) => {
console.log('I got a request');
console.log(request.body,"req");
response.json({
status: 'success',
latitude: request.body.lng,
longitude: request.body.lat
})
})
但我没有在请求正文中获取数据
listening at 3000
I got a request
{} req
请帮助我解决这个问题,因为它已经很长时间了,我无法调试它。
你的控制台登录 body 所以你应该发送 body
常量选项 = {
方法:'POST',
headers:{
'Content-Type': 'application/json'
},
body:JSON.stringify(数据)
};
您可能需要 body-parser 来处理 json body
https://www.npmjs.com/package/body-parser
var express = require('express')
var bodyParser = require('body-parser');
var app = express()
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
我正在客户端获取我的当前位置并且我正在使用 fetch。
if ('geolocation' in navigator) {
console.log('geolocation available');
navigator.geolocation.getCurrentPosition(async (position) => {
console.log(position.coords.latitude, position.coords.longitude);
let lat = position.coords.latitude;
let lng = position.coords.longitude;
document.getElementById('latitude').textContent = lat;
document.getElementById('longitude').textContent = lng;
const data = { lat, lng }
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify(data)
};
console.log(options, "options")
let response = await fetch('/api', options)
const datas = await response.json();
console.log(datas, 'after fetch');
});
} else {
console.log('geolocation not available')
}
在服务器端我正在做 post
const express = require("express");
const app = express();
app.use(express.static('public'));
app.use(express.json({ limit: '1mb' }));
// app.use(express.urlencoded());
app.listen(3000, () => console.log('listening at 3000'))
app.post('/api', (request, response) => {
console.log('I got a request');
console.log(request.body,"req");
response.json({
status: 'success',
latitude: request.body.lng,
longitude: request.body.lat
})
})
但我没有在请求正文中获取数据
listening at 3000
I got a request
{} req
请帮助我解决这个问题,因为它已经很长时间了,我无法调试它。
你的控制台登录 body 所以你应该发送 body
常量选项 = { 方法:'POST', headers:{ 'Content-Type': 'application/json' }, body:JSON.stringify(数据) };
您可能需要 body-parser 来处理 json body
https://www.npmjs.com/package/body-parser
var express = require('express')
var bodyParser = require('body-parser');
var app = express()
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());