簡體   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