簡體   English   中英

為什么我的 AJAX JQUERY 試圖將多個變量值傳遞給 php 服務器端總是返回錯誤?

[英]Why is my AJAX JQUERY trying to pass multiple variable values to php server side always returning an error?

我正在嘗試通過 JavaScript 從客戶端向服務器端發送多個值。 這是我的 JavaScript JQUERY function 下面:

// This function addToRentalCart(car) adds the requested
// car to a user's rental cart. It does this through the use
// of an AJAX call to the server with the required data.
// Since the required car data is already on the associated
// html webpage (index.html) all this function does is read it
// from there and send it to the server side process.
// Server side processing will then take care of the rest
// of the operation.

function addToRentalCart (car)
{
    var carPosition = 'carRow' + car;
    var carAvailabilityCol = 'Col10';
    var carAvailableElement = carPosition + carAvailabilityCol;
    var carAvailable = document.getElementById(carAvailableElement);
    
    if (carAvailable.innerHTML === 'N')
    {                                                         //If the car is not available
        alert("Sorry, the car is not available now. Please try other cars.");
    }
    else
    {                                                       //If the car is available
        //var carPictureFileCol = 'Col0';               //Set the column numbers of each desired variable
        var carMakeCol = 'Col1';
        var carModelCol = 'Col2';
        var carYearCol = 'Col3';
        var carPricePerDayCol = 'Col8';
        
        var carMakeElement = carPosition + carMakeCol;
        var carMake = document.getElementById(carMakeElement).innerHTML;   //Get the car make or brand
        
        var carModelElement = carPosition + carModelCol;
        var carModel = document.getElementById(carModelElement).innerHTML;      //Get the car model
        var carPictureFile = carModel + '.jpg';                           //Get the car picture file
        
        var carYearElement = carPosition + carYearCol;
        var carYear = document.getElementById(carYearElement).innerHTML;        //Get the car year
        
        var carPricePerDayElement = carPosition + carPricePerDayCol;
        var carPricePerDay = document.getElementById(carPricePerDayElement).innerHTML;
        carPricePerDay = carPricePerDay.substring(1);    //Get the price per day without the dollar sign
        $.ajax({
                type: "GET",
                url: "rentalCarsCart.php",
                data: {"carPicFile": carPictureFile, "carBrand": carMake, "carMod": carModel, 
                       "carYearMan": carYear, "carPPD": carPricePerDay},
                dataType: "json",
                success: function()
                {
                    alert("You have successfully added this car to your rental cart");
                },
                error: function()
                {
                    alert("error in Ajax call to cart url");
                },
            });
    }
}

即使根據 Apache Netbeans IDE Z78E6221F6393D1356Z81DB3 總是顯示錯誤警報,所有值似乎都很好。 我什至使用斷點和逐步方法在 Chrome 開發人員工具中對其進行了調試,所有值都很好。 它在 JQUERY.JS 內部轟炸,而不是向 PHP 后端發送 GET 請求。 有誰知道為什么? 我似乎找不到我的代碼有什么問題。 如果你能幫助我,我將不勝感激。

我的服務器端代碼如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Rental Car Shopping Cart</title>
        <style>
            h1 {text-align: center;}
            button:hover{cursor: pointer;}
        </style>
    <h1>Car Reservation</h1>
    </head>
    <body>
        <?php
            header("Access-Control-Allow-Origin: *");
            
            $carPicture = $_GET['carPicFile']; // get car picture filename
            $carMake = $_GET['carBrand'];     // get the make of the car
            $carModel = $_GET['carMod'];      // get the car model
            $carYear = $_GET['carYearMan']; // Get the car year of manufacture
            $carPricePerDay = $_GET['carPPD'];    // Get the car price per day
            echo ('<div>');
            echo ($carPricePerDay);
            echo ('</div>');
        ?>
    </body>
</html>

菲爾在上面的評論中是對的,您不需要來自 PHP 服務器端編碼的數據類型響應,所以我將其刪除。 我還發現了 PHP 后端的另一個問題,雖然上面的 PHP 代碼中沒有顯示,這最初是非常基本的,但后來變得更加復雜:

Instead of if (($carPicture!=0) && ($carMake!=0) && ($carModel!=0) && ($carYear!=0) && 
  ($carPricePerDay!=0))
            {
                session_start();         //etc
Try:  
if (isset($carPicture) && isset($carMake) && isset($carModel) && isset($carYear) && isset($carPricePerDay))
            {
                session_start();           //etc

原因是當變量未定義時,它們也等於零,或者我不確定為什么它決定使用底層版本。 PHP 的正邏輯似乎優於負邏輯。 多謝你們。

暫無
暫無

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

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