繁体   English   中英

POST请求返回index.html doc而不是反应js中的json数据

[英]POST request returns index.html doc instead of json data in react js

将我的反应 CRUD 应用程序部署到 cpanel 后,动态路由器上的 POST 请求

<Route exact path="/product/:code">
  <SingleProduct/> 
</Route> 

现在正在返回 index.html 而不是来自请求的 json 数据。 我收到 200 个状态码,但响应是 html。 但是,它在本地运行良好。 我使用 php 作为后端。除此之外,所有非动态路由都在工作。 我可以做些什么来解决这个问题?

这是 ajax。

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);
} 

我想出了解决方案。 当我在浏览器网络中进行调试时,我发现动态路由的请求 header 路径是:


 product/backend.php

代替


 backend.php

为了解决这个问题,我必须在路径中包含域名


 https://something.something/backend

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM