简体   繁体   中英

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

After deploying my react CRUD app to cpanel, the POST request on a dynamic router

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

is now returning index.html instead of json data from the request. I'm getting 200 status code but the response is html. However, it works fine locally. Am using php as the backend.All the non dynamic routes are working except this one. what can id do to fix this?

This is the 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}`);

This is the php code.

if(isset($_POST['result'])){
  $query = 'SELECT * FROM products'; 
  $result = mysqli_query($conn, $query);
  $users = mysqli_fetch_all($result, MYSQLI_ASSOC);  
  echo json_encode($users);
} 

I figured out the solution. When i did debugging in my browser network, i found out the request header path for dynamic route was:


 product/backend.php

instead of


 backend.php

To solve this i had to include the domain name in the path


 https://something.something/backend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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