这是从我的本地文件调用 PHP 函数的 ajax-jquery 请求。
如何在 javascript 中获取 'data: { functionname: 'getIntervenant', arguments: [] }' 的结果?
jQuery.ajax({
type: "POST",
url: 'http://localhost:8012/extension-Oasis/php/getIntervenant.php',
dataType: 'json',
data: { functionname: 'getIntervenant', arguments: [] },
success: function (obj, textstatus) {
if( !('error' in obj) ) yourVariable = obj.result;
else console.log(obj.error);
}
});
这是我的 php 函数:
function getIntervenant(){
$CI = 'EDIFICE';
$stmt = $conn->prepare("SELECT ID,Nom FROM intervenant WHERE nextAffect='x' and CI=:ci");
$stmt->bindParam(":ci",$CI);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_NUM)){
$id = $row[0];
$name = $row[1];
}
return $name;
}
最佳答案
而不是 return
使用 echo
像:
echo $name;
在 js 部分,您将收到如下结果:
$.ajax({
type: "POST",
url: 'http://localhost:8012/extension-Oasis/php/getIntervenant.php',
dataType: 'json',
data: { functionname: 'getIntervenant', arguments: [] },
).done(function( name ) {
console.log(name);
});
关于javascript - 如何从 ajax-jquery 请求中获取 php 函数的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634358/