AJAX inside $.when 给出 return 字符串的第一个字符
AJAX inside $.when giving fist character of the return string
我正在使用 $.when 调用 AJAX 等待 ajax 完成并 return 处理内部的下一个 ajax。
这是 $.when 呼叫发生的地方:
function loadAllData(){
$.when(getCreditorID()).done(function(a1){
console.log("cx id is : " + parseFloat(a1[0])); //this is in the attached screen shot
var urlx = "functions/getCustomerData.php";
$.post(
urlx,
{
selectedValue: a1[0],
},
function(data) {
$("#payduedate").val(data[0].duedate);
document.getElementById('portcode').value = data[0].portcode;
document.getElementById('currencycode').value = data[0].currencycode;
document.getElementById('convertion').value = data[0].conversion;
},
"json"
);
});
}
上面的代码调用下面的ajax方法函数:
function getCreditorID(){
id = "";
var creditorcodex = document.getElementById('creditorcode').value;
// console.log("getCreditorID input: " + creditorcodex);
var urlx = "functions/getCreditorID.php";
return $.ajax({
type: 'POST',
url: urlx,
data: {
creditorcode: creditorcodex,
},
success: function(data) {
console.log("Result : "+data); //this is in the attached screen
}
});
}
以上函数调用getCreditorID.php获取数据:
getCreditorID.php:
<?php
include '../config/dbConn.php';
$creditorcode = $_POST["creditorcode"];
// $creditorcode = $_GET["creditorcode"];
$result="";
$sql = "SELECT intCustomerID FROM lms.tblcustomers WHERE varCustomerName='".$creditorcode."';";
mysql_select_db('$dbname');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
$result=-999;
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$result=$row["intCustomerID"];
}
echo $result;
mysql_close($conn);
?>
问题是:
如果 getCreditorID.php 中的 return 为“44”,则 getCreditorID() 函数中的 console.log("Result : "+data);
将在控制台中输出为 'Result : 44',并且工作正常。但是相同的函数在 loadAllData() 函数中得到 returned,并为下一个 ajax 使用值 returned。在这里,如果我们使用 console.log("cx id is : " + parseFloat(a1[0]));
打印 return 值,则输出为“4”,应该是“44”。这意味着它只给出第一个字符作为输出并忽略其余字符。
运行 控制台的屏幕截图:
请想办法出去。
在您的函数 loadAllData() 中,使用 a1 而不是 a1[0] 并相应地更新您的代码
function loadAllData(){
$.when(getCreditorID()).done(function(a1){
console.log("cx id is : " + parseFloat(a1)); // did correction here
var urlx = "functions/getCustomerData.php";
$.post(
urlx,
{
selectedValue: a1[0],
},
function(data) {
$("#payduedate").val(data[0].duedate);
document.getElementById('portcode').value = data[0].portcode;
document.getElementById('currencycode').value = data[0].currencycode;
document.getElementById('convertion').value = data[0].conversion;
},
"json"
);
});
}
我正在使用 $.when 调用 AJAX 等待 ajax 完成并 return 处理内部的下一个 ajax。
这是 $.when 呼叫发生的地方:
function loadAllData(){
$.when(getCreditorID()).done(function(a1){
console.log("cx id is : " + parseFloat(a1[0])); //this is in the attached screen shot
var urlx = "functions/getCustomerData.php";
$.post(
urlx,
{
selectedValue: a1[0],
},
function(data) {
$("#payduedate").val(data[0].duedate);
document.getElementById('portcode').value = data[0].portcode;
document.getElementById('currencycode').value = data[0].currencycode;
document.getElementById('convertion').value = data[0].conversion;
},
"json"
);
});
}
上面的代码调用下面的ajax方法函数:
function getCreditorID(){
id = "";
var creditorcodex = document.getElementById('creditorcode').value;
// console.log("getCreditorID input: " + creditorcodex);
var urlx = "functions/getCreditorID.php";
return $.ajax({
type: 'POST',
url: urlx,
data: {
creditorcode: creditorcodex,
},
success: function(data) {
console.log("Result : "+data); //this is in the attached screen
}
});
}
以上函数调用getCreditorID.php获取数据: getCreditorID.php:
<?php
include '../config/dbConn.php';
$creditorcode = $_POST["creditorcode"];
// $creditorcode = $_GET["creditorcode"];
$result="";
$sql = "SELECT intCustomerID FROM lms.tblcustomers WHERE varCustomerName='".$creditorcode."';";
mysql_select_db('$dbname');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
$result=-999;
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
$result=$row["intCustomerID"];
}
echo $result;
mysql_close($conn);
?>
问题是:
如果 getCreditorID.php 中的 return 为“44”,则 getCreditorID() 函数中的 console.log("Result : "+data);
将在控制台中输出为 'Result : 44',并且工作正常。但是相同的函数在 loadAllData() 函数中得到 returned,并为下一个 ajax 使用值 returned。在这里,如果我们使用 console.log("cx id is : " + parseFloat(a1[0]));
打印 return 值,则输出为“4”,应该是“44”。这意味着它只给出第一个字符作为输出并忽略其余字符。
运行 控制台的屏幕截图:
请想办法出去。
在您的函数 loadAllData() 中,使用 a1 而不是 a1[0] 并相应地更新您的代码
function loadAllData(){
$.when(getCreditorID()).done(function(a1){
console.log("cx id is : " + parseFloat(a1)); // did correction here
var urlx = "functions/getCustomerData.php";
$.post(
urlx,
{
selectedValue: a1[0],
},
function(data) {
$("#payduedate").val(data[0].duedate);
document.getElementById('portcode').value = data[0].portcode;
document.getElementById('currencycode').value = data[0].currencycode;
document.getElementById('convertion').value = data[0].conversion;
},
"json"
);
});
}