node.js - 使用 axios 响应原生 api 调用
问题描述
我有 nodeJs 后端,我在其中创建了这个 api
app.post("/addInvoice", (req, res) => {
let invoice = req.body;
var sql =
"INSERT INTO invoice (address, email, billnumber, price, status) VALUES(?, ?, ?, ?, 'sent');";
mysqlConnection.query(
sql,
[
invoice.address,
invoice.customeremail,
invoice.billnumber,
invoice.totalprice,
],
(err, rows, fields) => {
if (!err) {
res.send(rows);
} else {
res.send(err);
}
}
);
});
当我使用邮递员调用此 API 时,它运行良好。
但现在我需要在我的 react-native 应用程序中通过 buttonclick 调用它。
axios
.post("http://localhost:3000/addInvoice", {
address: address,
customeremail: email,
billnumber: invoicenumber,
totalprice: totalPrice,
})
.then((response) => {
alert(response);
});
当我发送这个并且警报(响应)什么都不返回时,我什么也没有发生。
可能是什么问题呢?
解决方案
您的模拟器(或电话)是一个独立的设备,与您的网络浏览器、邮递员或服务器不在同一 IP(本地主机或 127.0.0.1)上运行。
为了从您的模拟器(或电话)向您的服务器发出请求,您需要通过您的计算机 IP 地址访问您的服务器:在 Windows 上,通过在命令提示符下运行 ipconfig 获取您的 IP 地址,或者在这里:系统偏好设置 > 网络 >高级 > TCP/IP > IPv4 地址。
而不是 http://localhost:port 你应该使用 http://your_ip_address:port
推荐阅读
- ios - swift 5中没有孩子时Coredata删除父记录
- spring - Mule 注册表 [Mule 4] 中未定义元素“springService”
- javascript - 有没有办法使从 d3.csv 函数加载的数据全局化?
- laravel - 无法使用 Goutte Laravel 包抓取此站点。元素存在于视图源中
- email - MJML 在 Outlook 中无法正确输出
- excel - 过滤表以返回值
- android - 不兼容的类型 androidx 观察者方法
- javascript - JavaScript 倒数计时器从零开始计数
- json - JSON 到 CSV - 深入 4 级
- svg - 当您更改指针事件时,Svg texpath 在 Firefox 中消失