簡體   English   中英

PHP沒有收到ajax發布數據

[英]php not receiving ajax post data

我正在嘗試通過AJAX將數據發送到php文件。 我可以使它與GET方法一起正常工作,但不能與POST方法一起工作(我的最終文件可能會變大,所以我想使用POST)。

使用Javascript

函數update_table(){var mydrop = document.getElementById('dropdown')。value;

Request1 = new XMLHttpRequest();
if (Request1) {
    var RequestObj1 = document.getElementById('Target1');
    Request1.onreadystatechange = function () {
      if (Request1.readyState == 4 && Request1.status == 200) {
       // document.getElementById('Target1').innerHTML = "test"; 
        RequestObj1.innerHTML = Request1.responseText;
      }
    }

    Request1.open("POST", "table.php"); 
    Request1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//  var url = "table.php?brand="+mydrop;
//  Request1.open('GET', url);  // this works fine
    Request1.send("brand" + mydrop);
} // end Request1 function

}

我的PHP

<?php
// header('Content-Type: text/xml')
ini_set('display_errors',1); 
 error_reporting(E_ALL);

 if(isset($_POST['brand'])) {
    $brandAccess = $_POST['brand'];

 }

 if(isset($_GET['brand'])) {
    $brandAccess = $_GET['brand'];

 }
print_r($_POST);
print_r($_GET);


include('./includes/connection.inc.php');
$conn = dbConnect();
$sql = "SELECT * from finished_goods WHERE BrandDesc LIKE '{$brandAccess}%' "; 

$result = $conn->query($sql);

?>

<table>
    <?php foreach ($result as $row) { ?>
    <tr>
        <td><?php echo $row['ProductNo'] ?></td>
        <td><?php echo $row['ProductName'] ?></td>
        <td><?php echo $row['BrandDesc'] ?></td>
        <td><?php echo $row['QtyOnHand'] ?></td>
    </tr>
    <?php } ?>
</table>

我把print_r函數放在那里只是為了仔細檢查,而POST是空的不管我嘗試什么。

注意,我以為我可能需要header('Content-Type:text / xml'),但是當我把它放進去時,該頁面根本無法工作。

謝謝,

嘗試

Request1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
Request1.send("brand=" + mydrop);

並在打印$ _POST之后設置模具。 在chrome dev-tools中查看您對php進行哪種查詢。

you must forget the famous header ajax json for jquery or another
(only necessary for php but essenssial for a good response serveur)
<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data);

**https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script**

( if errors, you must write this first, before one echo html.. (and no echo html is good for great json response)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM