[英]how to return an integer value from a php file using ajax method of jquery?
我写了一个php代码来查找目录中jpg文件的数量,这是代码。
<?php
$directory = $_POST['address'];
$num_files = 0;
$files = glob($directory."*.jpg");
$num_files = count($files);
echo $num_files;
?>
而且我正在使用jquery的ajax方法发送和接收来自php文件的数据。 代码如下。
$(document).ready(function(){
$('.mainnav li ul li').click(function(){
var parent_id = $(this).parent().attr('id');
var index = $(this).index();
var address = "images/"+parent_id+"/"+index+"/";
/*finding the number of files in the address directory*/
var number_of_files;
$.ajax({
type:"POST",
url: "numberoffiles.php",
data:{address:address},
success: function(data){
number_of_files = data;/*Line label:A*/
alert(number_of_files);
}
});
alert(number_of_files+2); /*this line is just for testing*/
/*end of number of files*/
for(var i=0;i<number_of_files;i++)
$('.thumbnail').append('<img src="'+address+i+'.jpg"></img>');
});
});
我在警报窗口中正确获取了“ number_of_files”的值,没有任何错误,但是我无法将其数据类型转换为整数。
我尝试了不同的方法来将从php文件中获取的数据转换为整数,但是对我没有任何帮助。
以下是我尝试过的。
number_of_files = parseInt(data);/*Line label:A*/
使用上面的代码行,我可以在警报框中正确获取“ number_of_files”的值,但使用“ number_of_files”进行的任何算术运算都将导致NAN。 例如alert(number_of_files+2);
返回一个NAN。
我也尝试了以下方法。
number_of_files = parseInt(data.data);/*Line label:A*/
这行代码返回一个NAN。 我也尝试过+ data和Number(data),但它们也不起作用。 所以,请告诉我我该怎么做。
您的问题是$ .ajax是一个异步方法,这意味着将执行成功函数,但您不知道何时执行,这还意味着ajax函数调用后的代码将立即执行。
您有2个解决方案:
在Ajax选项中将“ async”设置为false(不推荐)
将所有“ post ajax代码”移至成功函数中
附带说明一下,javascript变量类型是动态的,这意味着JS引擎将根据需要转换变量。
编辑:第二个选项的一个示例:
$(document).ready(function(){
$('.mainnav li ul li').click(function(){
var parent_id = $(this).parent().attr('id');
var index = $(this).index();
var address = "images/"+parent_id+"/"+index+"/";
/*finding the number of files in the address directory*/
var number_of_files;
$.ajax({
type:"POST",
url: "numberoffiles.php",
data:{address:address},
success: function(data){
number_of_files = data;
alert(number_of_files);
for(var i=0;i<number_of_files;i++)
$('.thumbnail').append('<img src="'+address+i+'.jpg"></img>');
}
});
});
});
success: function(data){
number_of_files = data;
alert(number_of_files);
alert(Number(number_of_files)+2); /*this line is just for testing*/
for(var i=0;i<number_of_files;i++)
$('.thumbnail').append('<img src="'+address+i+'.jpg"></img>');
}
用户parseInt()函数可在成功函数内部将字符串转换为整数,仅在成功函数中使用number_of_files
进行调用。
success: function(data){
number_of_files = parseInt(data);
alert(number_of_files);
}
$(document).ready(function(){
$('.mainnav li ul li').click(function(){
var parent_id = $(this).parent().attr('id');
var index = $(this).index();
var address = "images/"+parent_id+"/"+index+"/";
/*finding the number of files in the address directory*/
$.ajax({
type:"POST",
url: "numberoffiles.php",
data:{address:address},
success: function(data){
number_of_files = data;
alert(number_of_files);
}
}).done(function(){ /*this line is just for testing*/
/*end of number of files*/
for(var i=0;i<number_of_files;i++)
$('.thumbnail').append('<img src="'+address+i+'.jpg"></img>');
});
});
});
//除了使用ajax编写代码,您还可以使用.done方法,有关更多信息,请参见http://api.jquery.com/jquery.ajax/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.