我的 express.js 没有收到我从 React Native 发送的 body
My express.js is not receiving the body that I sent from react native
我在 React Native 中有一个 api object 设置:
import axios from "axios";
import AsyncStorage from "@react-native-async-storage/async-storage"; //npm install @react-native-async-storage/async-storage
const instance = axios.create({
baseURL: "localhost url here",
});
/**
* This will add a header if we have a token only,
* we will be adding a Authorization header to our instance before running
* the http req
*/
instance.interceptors.request.use(
//this will be called before doing the http request,
//it is async because to retrieve the storage it is async
async (config) => {
const token = await AsyncStorage.getItem("token"); //awaits until it gets token (IF THERE IS ONE)
//if there is a token
if (token) {
config.headers.Authorization = `Bearer ${token}`; //add string 'Bearer withGivenTOKEN'
}
return config;
},
(err) => {
return Promise.reject(err);
}
);
export default instance;
在进行 api 调用时,我是这样做的:
await myApi
.get("/routeHere", {
latitude: currentLocation.latitude,
longitude: currentLocation.longitude,
})
.then((response) => console.log(response))
.catch((err) => console.log(err));
接收数据时,body 部分只是一个空 object。发生这种情况有原因吗?我做错了什么吗?
router.get("/routeHere", async (req, res) => {
console.log("here is my body: ", req.body);
}
我想我缺少添加 header 类型,但不确定它是否有效,如果有效,你怎么写?我是新来的表达和反应原生
GET 请求不应包含数据。
The HTTP GET method requests a representation of the specified resource. Requests using GET should only be used to request data (they shouldn't include data).
但是你可以使用params来发送纬度和经度,像这样:
await myApi
.get(`/routeHere?latitude=${currentLocation.latitude}&longitude=${currentLocation.longitude}`)
.then((response) => console.log(response))
.catch((err) => console.log(err));
或
await myApi
.get("/routeHere", {
params: {
latitude: currentLocation.latitude,
longitude: currentLocation.longitude,
}
})
.then((response) => console.log(response))
.catch((err) => console.log(err));
并且您可以在后端使用 req.query.latitude
和 req.query.longitude
接收它
我在 React Native 中有一个 api object 设置:
import axios from "axios";
import AsyncStorage from "@react-native-async-storage/async-storage"; //npm install @react-native-async-storage/async-storage
const instance = axios.create({
baseURL: "localhost url here",
});
/**
* This will add a header if we have a token only,
* we will be adding a Authorization header to our instance before running
* the http req
*/
instance.interceptors.request.use(
//this will be called before doing the http request,
//it is async because to retrieve the storage it is async
async (config) => {
const token = await AsyncStorage.getItem("token"); //awaits until it gets token (IF THERE IS ONE)
//if there is a token
if (token) {
config.headers.Authorization = `Bearer ${token}`; //add string 'Bearer withGivenTOKEN'
}
return config;
},
(err) => {
return Promise.reject(err);
}
);
export default instance;
在进行 api 调用时,我是这样做的:
await myApi
.get("/routeHere", {
latitude: currentLocation.latitude,
longitude: currentLocation.longitude,
})
.then((response) => console.log(response))
.catch((err) => console.log(err));
接收数据时,body 部分只是一个空 object。发生这种情况有原因吗?我做错了什么吗?
router.get("/routeHere", async (req, res) => {
console.log("here is my body: ", req.body);
}
我想我缺少添加 header 类型,但不确定它是否有效,如果有效,你怎么写?我是新来的表达和反应原生
GET 请求不应包含数据。
The HTTP GET method requests a representation of the specified resource. Requests using GET should only be used to request data (they shouldn't include data).
但是你可以使用params来发送纬度和经度,像这样:
await myApi
.get(`/routeHere?latitude=${currentLocation.latitude}&longitude=${currentLocation.longitude}`)
.then((response) => console.log(response))
.catch((err) => console.log(err));
或
await myApi
.get("/routeHere", {
params: {
latitude: currentLocation.latitude,
longitude: currentLocation.longitude,
}
})
.then((response) => console.log(response))
.catch((err) => console.log(err));
并且您可以在后端使用 req.query.latitude
和 req.query.longitude