首页 > 解决方案 > 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);
} 

标签: javascriptphpajax

解决方案


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


 product/backend.php

代替


 backend.php

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


 https://something.something/backend

推荐阅读