javascript - 为什么我的 AJAX JQUERY 试图将多个变量值传递给 php 服务器端总是返回错误?
问题描述
我正在尝试通过 JavaScript 将多个值从客户端发送到服务器端。下面是我的 JavaScript JQUERY 函数:
// 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 输出,所有值似乎都很好,但它会爆炸并始终显示错误警报。我什至使用断点和逐步方法在 Chrome 开发人员工具中对其进行了调试,所有值都很好。它在 JQUERY.JS 本身内部轰炸,而不是将 GET 请求发送到 PHP 后端。有谁知道为什么?我似乎找不到我的代码有什么问题。如果你能帮助我,我将不胜感激。
我的服务器端代码如下:
<!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>
解决方案
Phil 在上面的评论中是正确的,您不需要来自 PHP 服务器端编码的 dataType 响应,因此我将其删除。我还发现了 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 的负逻辑更好。多谢你们。
推荐阅读
- javascript - 如何减慢发出请求的速度?
- excel - 基于VBA中字符串列中的字符应用过滤器
- r - 如何使用 ggplot2 绘制累积频率线图?
- tkinter - 如何从 Tkinter 程序中打开默认的 Ubuntu 文本编辑器?
- xslt - XSLT 基于值对出现进行分组
- javascript - 如何在 Tabulator.js 中为新列添加数据?
- php - PHP嵌套数组创建
- python-3.x - 在 gcloud 应用引擎上运行 Python Flask 应用程序的延迟非常高
- django - 如何在 django 模板中以 1-7 天(周)的格式获取日期值
- c# - 加载时基于 Combobox 索引的方法