javascript - POST 请求返回 index.html doc 而不是 react js 中的 json 数据
问题描述
将我的反应 CRUD 应用程序部署到 cpanel 后,动态路由器上的 POST 请求
<Route exact path="/product/:code">
<SingleProduct/>
</Route>
现在从请求中返回 index.html 而不是 json 数据。我收到 200 个状态码,但响应是 html。但是,它在本地运行良好。我使用 php 作为后端。除此之外,所有非动态路由都在工作。我可以做些什么来解决这个问题?
这是阿贾克斯。
var xhr = new XMLHttpRequest();
xhr.open('POST', backend.php,true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function(){
const users =JSON.parse(this.responseText); console.log(users);
dispatch(fetchperfumes(users))
}
xhr.send(`result=${result}`);
这是php代码。
if(isset($_POST['result'])){
$query = 'SELECT * FROM products';
$result = mysqli_query($conn, $query);
$users = mysqli_fetch_all($result, MYSQLI_ASSOC);
echo json_encode($users);
}
解决方案
我想出了解决方案。当我在浏览器网络中进行调试时,我发现动态路由的请求标头路径是:
product/backend.php
代替
backend.php
为了解决这个问题,我必须在路径中包含域名
https://something.something/backend
推荐阅读
- session - 未在 Edge 上设置会话 cookie(点网核心)
- java - 来自android studio的应用程序在我的手机上不断崩溃
- jenkins - “仅在 SCM 更改时构建”在 jenkins 作业中设置为 true,但不使用最新提交构建
- c# - 将元组传递给 ObjectDataProvider 参数
- r - 从 R 中的数据框列表中删除特定的数据框
- python - AttributeError:'float' 对象在 pandas 中没有属性'split'
- javascript - 前馈神经网络的输出接近 0
- docker - docker-compose 只读绑定卷合并
- audit.net - 在我的应用程序的“Startup.cs”中注册 Audit.Net 的第一步是什么?
- c# - C# WebClient - 非常简单的将 json 发布到 PHP 服务器