Php 异步javascript问题,即使网络有响应,仍将未定义的追加到页面
我对从ajax调用中获取的值有一个问题,该调用试图在实际分配值之前附加到页面。例如,当下面的sendRequest被调用时,我每次都会从网络中得到一个价格响应,但当我将其输入console.log时,有时会得到未定义的响应。请参见粗体注释。我已经尝试过使ajax请求同步,并使用一个真正的隔离区setTimeout函数,但仍然没有定义偶然性。我如何确保价格附加到页面上?如果我今天行动迟缓,请对我好一点:DPhp 异步javascript问题,即使网络有响应,仍将未定义的追加到页面,php,jquery,settimeout,Php,Jquery,Settimeout,我对从ajax调用中获取的值有一个问题,该调用试图在实际分配值之前附加到页面。例如,当下面的sendRequest被调用时,我每次都会从网络中得到一个价格响应,但当我将其输入console.log时,有时会得到未定义的响应。请参见粗体注释。我已经尝试过使ajax请求同步,并使用一个真正的隔离区setTimeout函数,但仍然没有定义偶然性。我如何确保价格附加到页面上?如果我今天行动迟缓,请对我好一点:D function updatePrices(IDs,callback){ var p
function updatePrices(IDs,callback){
var product_id= <?=$product_id ?>;
var qty= parseInt($j("#qtyUpdateBox input").val());
var customer_id = null;
<?php if (isset($customer_id)) {?>
customer_id = <?=$customer_id?>;
//$j('#vendorPriceListHeading').css({
//'width': '230px',
//'margin-left':'105px',
//'margin-right':'-80px'
// });
<?php }?>
if (qty==1){
function sendRequestOne(i) {
var optionSelectionArray = currentlySelectedAttributes(IDs[i]);
simpleWithAttrPrice(optionSelectionArray, customer_id, qty, function(data) {
var data= JSON.parse(data);
var unitPrice = parseFloat(roundDollar(data.basePrice));
$j('.details'+IDs[i]+ ' .priceBlock').empty();
$j('.details'+IDs[i]+ ' .priceBlock').append('<span>'+formatCurrency(unitPrice,"$")+'</span>');
$j('.details'+IDs[i]+ ' .vendorCheck input[name="customPrice"]:checked').val(unitPrice);
});
}//end sendRequest
for(i=0; i<IDs.length; i++)
{
sendRequestOne(i);
}
}//end if
else{
//ajax call to obtain tier prices for each vendor id
function sendRequest(i,qty,product_id){
var vendor = IDs[i];
$j.ajax({
type: "POST",
url: "/ajax_calls/updatePrices.php",
async:false,
data: { 'vendorID': vendor, 'product_id': product_id}
}).done(function(data) {
//CAITLIN below may need to be parsed in the php script
var data= JSON.parse(data);
var optionSelectionArray = currentlySelectedAttributes(vendor);
simpleWithAttrPrice(optionSelectionArray, customer_id, qty, function(price) {
var price= JSON.parse(price);
var unitPrice = roundDollar(parseFloat(price.basePrice));
var pricexQty= unitPrice * qty;
if (qty < data.tier2_range_start){
var unitPrice = totalPrice/qty;
}
else if (qty >= data.tier2_range_start && qty < data.tier3_range_start){
var discountPercent = data.tier2_discount;
var discount = pricexQty * data.tier2_discount / 100;
var totalPrice = pricexQty - discount;
}
else if (qty >= data.tier3_range_start && qty < data.tier4_range_start){
var discountPercent = data.tier3_discount;
var discount = pricexQty * data.tier3_discount / 100;
var totalPrice = pricexQty - discount;
}
else if (qty >= data.tier4_range_start && qty < data.tier5_range_start){
var discountPercent = data.tier4_discount;
var discount = pricexQty * data.tier4_discount / 100;
var totalPrice = pricexQty - discount;
}
else if (qty >= data.tier5_range_start){
var discountPercent = data.tier5_discount;
var discount = pricexQty * data.tier5_discount / 100;
var totalPrice = pricexQty - discount;
}
else{
console.log('Something went wrong');
}
var unitPrice = roundDollar(totalPrice/qty); //unitPrice including Shipping
setTimeout(function(){
//BELOW IS LOGGING UNDEFINED SOMETIMES, BUT AJAX RESPONSE HAS THE VALUES
console.log("The unit price is " + unitPrice + " and the discount percent is " + discountPercent);
$j('.details'+vendor+ ' .priceBlock').empty();//update product price in DOM
$j('.details'+vendor+ ' .priceBlock').append('<span>'+formatCurrency(unitPrice,"$")+'</span>');
//$j('.details'+data.vendor_id+ ' .priceBlock').append('<span>Total Price: '+formatCurrency(unitPrice*qty,"$")+'</span>');
$j('.details'+vendor+ ' .vendorCheck input[name="customPrice"]:checked').val(unitPrice);
$j('.details'+vendor+ ' .priceBlock').append('<h5 style="color:green">You will save '+discountPercent+'% !</h5>');
},1000);
});//end callback function
//reorderByPrice();
});//end done function
}//end function sendRequest
for(i=0; i<IDs.length; i++)
{
sendRequest(i,qty,product_id);
}
}//end else
if (callback) {
setTimeout(callback, 1);
}
}//end function
function simpleWithAttrPrice(optionSelectionArray, customer_id, qty, callback){
var product_id= <?=$product_id ?>;
$j.ajax({
type: "POST",
url: "/ajax_calls/obtainBasePrice.php",
data: { 'productID': product_id, 'optionSelectionArray' : optionSelectionArray, 'customer_id': customer_id, 'qty': qty}
}).done(callback);
}
函数更新许可证(ID、回调){
var乘积_id=;
var qty=parseInt($j(“#qtyUpdate框输入”).val();
var customer_id=null;
客户_id=;
//$j(“#供应商价格分类”).css({
//“宽度”:“230px”,
//“左边距”:“105px”,
//“右边距”:“-80px”
// });
如果(数量=1){
函数sendRequestOne(i){
var OptionSelectionAry=当前选定属性(ID[i]);
simpleWithAttrPrice(选项选择栏、客户id、数量、功能(数据){
var data=JSON.parse(数据);
var单价=parseFloat(roundDollar(data.basePrice));
$j('.details'+IDs[i]+'.priceBlock').empty();
$j('.details'+IDs[i]+'.priceBlock')。追加(''+formatCurrency(unitPrice,“$”)+'');
$j('.details'+IDs[i]+'.vendorCheck输入[name=“customPrice”]:选中').val(单价);
});
}//结束发送请求
对于(i=0;i=data.tier2\u range\u start&数量=data.tier3\u range\u start&&qty=data.tier4\u range\u start&&qty=数据。第5层\u范围\u开始){
var折扣百分比=data.tier5\u折扣;
var折扣=价格xQTY*data.tier5\U折扣/100;
var totalPrice=价格xQTY-折扣;
}
否则{
log(“出了点问题”);
}
var unitPrice=roundDollar(总价/数量);//单价包括运费
setTimeout(函数(){
//下面的日志有时未定义,但AJAX响应具有以下值
console.log(“单价为“+单价+”,折扣百分比为“+折扣百分比”);
$j('.details'+vendor+'.priceBlock').empty();//在DOM中更新产品价格
$j('.details'+vendor+'.priceBlock')。追加(''+formatCurrency(单价,“$”)+'');
//$j('.details'+data.vendor_id+'.priceBlock').append('总价:'+格式货币(单价*数量,“$”)+”);
$j('.details'+vendor+'.vendorCheck输入[name=“customPrice”]:选中]).val(单价);
$j('.details'+vendor+'.priceBlock').append('您将保存'+discountPercent+'%!');
},1000);
});//结束回调函数
//reorderByPrice();
});//结束函数
}//结束函数sendRequest
对于(i=0;i
响应屏幕截图(所有ajax调用都返回正确的值):
我想,的
向您的服务器生成了太多请求。因此服务器响应可能不是200。您应该查找该响应的属性error
,然后单击try movesetTimeout(…)…
到错误
部分。正如您所说,它在90%的时间内工作正常,因此我假设如果块未执行,则在90%的时间内执行,并且在其他10%的时间内执行它
if (qty < data.tier2_range_start){
var unitPrice = totalPrice/qty;
}
此时,discountPercent
是未定义的
,因为您在第一个块中没有计算它,如果块我不知道,我想它会给它时间赋值???只是一个微弱的尝试。无论它是否存在,都会发生同样的事情。不确定它是否与问题有关,但您似乎处于错误状态他认为javascript对您的变量有块作用域,这一行:console.log(“单价为“+unitPrice+”,折扣百分比为“+discountPercent”);有90%的时间是正确的值,另外10%的时间是未定义的,所以我认为这不是问题所在……我认为这与ajax请求无关,而与ajax请求返回的内容无关。simpleWithThatPrice
do做什么?我在上面用函数编辑过。可能是“}if(回调){setTimeout”的位置(回调,1);}”
if (qty < data.tier2_range_start){
var unitPrice = totalPrice/qty;
}
console.log("The unit price is " + unitPrice + " and the discount percent is " + discountPercent);