我不确定如何将php函数结果返回给jQuery ajax请求我的functions.js文件中包含以下函数:
var PartNumber = "123456890";
$.ajax({url: 'aoiprocedures?Action=loadparsedata&FullPartNumber=' + PartNumber,
success: function(oData){
alert(oData);
}});
我的aoiprocedures.php文件中包含以下内容:
<?php
$pcAction = isset( $_REQUEST['Action'] ) ? $_REQUEST['Action'] : "";
$FullPartNumber = isset( $_REQUEST['FullPartNumber'] ) ? $_REQUEST['FullPartNumber'] : 0 ;
switch($pcAction){
case "loadparsedata":
$GenerateMsg = DoParseFile($FullPartNumber);
break;
}
function DoParseFile($FullPartNumber){
//do a bunch of stuff//
return $FullPartNumber ;
};
?>
所以,出于测试目的,我应该回到我的javascript alert()框“1234567890”,但我得到“”(空白)。有什么想法吗?
答案 0 :(得分:4)
正如CoolStraw已经说过的那样,ajax看到的响应与您在浏览器中看到的完全相同。因此,除非你打印出来,否则你会得到一个空白的答案。
因此...
变化:
return $FullPartNumber ;
要:
echo $FullPartNumber ;
同样改变:
$GenerateMsg = DoParseFile($FullPartNumber);
要:
DoParseFile($FullPartNumber);
答案 1 :(得分:2)
您必须回显或打印您的结果。从您的php文件中发出的内容,就像您通过浏览器访问它时的内容一样,结果将被您的ajax调用接收
答案 2 :(得分:0)
尝试:
<script type="text/javascript">
var PartNumber = "123456890";
$.post("aoiprocedures", { my_action: "loadparsedata", FullPartNumber: PartNumber} , success: function(oData){
alert(oData);
});
</script>
<?php
function DoParseFile($FullPartNumber){
//do a bunch of stuff//
echo $FullPartNumber ;
};
$pcAction = isset( $_POST['my_action'] ) ? $_REQUEST['my_action'] : "";
$FullPartNumber = isset( $_REQUEST['FullPartNumber'] ) ? $_REQUEST['FullPartNumber'] : 0 ;
switch($pcAction){
case "loadparsedata":
$GenerateMsg = DoParseFile($FullPartNumber);
break;
}
?>