我有一个问题 console.logging 从我的 jquery ajax 调用发送回脚本的 json 对象的值。
剧本:
for(i=0; i<IDs.length; i++)
{
var vendor = IDs[i];
$j.ajax({
type: "POST",
url: "/ajax_calls/updatePrices.php",
data: { 'vendorID': vendor, 'product_id': product_id}
}).done(function(data) {
console.log('The data is ' + data);
var basePrice = simpleArray[vendor][colorSelected];
//if(data.tier2_range_start[i])
console.log('Range start is data.tier2_range_start: ' + data.tier2_range_start);
if (qty < data.tier2_range_start){
simpleArray[vendor][colorSelected]= basePrice * qty;
}
else if (qty > data.tier2_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier2_discount;
}
else if (qty > data.tier3_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier3_discount;
}
else if (qty > data.tier4_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier4_discount;
}
else if (qty > data.tier5_range_start){
simpleArray[vendor][colorSelected]= (basePrice * qty) * data.tier5_discount;
}
else{
console.log('Something went wrong');
}
$j('.details'+vendor+ ' .priceBlock').empty();
$j('.details'+vendor+ ' .priceBlock').append('<span>'+simpleArray[vendor][colorSelected]+'</span>');
});
}//end for
脚本调用:
<?php
require_once('/var/www/Staging/connect.php');
//post variable
$ID= $_POST['vendorID'];
$product_id= $_POST['product_id'];
$echoArray= array();
//if ( !isset($echoArray[$x]) )
//$echoArray[$x] = array();
$sql = 'SELECT * FROM tier_pricing WHERE vendor_id=' . $ID. ' AND product_id=' . $product_id;
foreach ($con->query($sql) as $row) {
$echoArray['vendor_id']= $row['vendor_id'];
$echoArray['tier2_range_start']= $row['tier2_range_start'];
$echoArray['tier2_range_stop']= $row['tier2_range_stop'];
$echoArray['tier3_range_start']= $row['tier3_range_start'];
$echoArray['tier3_range_stop']= $row['tier3_range_stop'];
$echoArray['tier4_range_start']= $row['tier4_range_start'];
$echoArray['tier4_range_stop']= $row['tier4_range_stop'];
$echoArray['tier5_range_start']= $row['tier5_range_start'];
$echoArray['tier2_discount']= $row['tier2_discount'];
$echoArray['tier3_discount']= $row['tier3_discount'];
$echoArray['tier4_discount']= $row['tier4_discount'];
$echoArray['tier5_discount']= $row['tier5_discount'];
}
echo json_encode($echoArray);
?>
数据记录为(例如,在没有空数据对象的循环上):
[15:06:13.397] The data is {"vendor_id":"3","tier2_range_start":"5","tier2_range_stop":"20","tier3_range_start":"20","tier3_range_stop":"100","tier4_range_start":"100","tier4_range_stop":"500","tier5_range_start":"500","tier2_discount":"2","tier3_discount":"3.1","tier4_discount":"4.3","tier5_discount":"5"}
为什么 data.tier2_range_start 未定义?