在将 php 数据返回到 jQuery ajax 调用时遇到问题


Having trouble returning php data to jQuery ajax call

似乎这种类型的问题以前已经被问过很多次了,但我似乎无法解决这个问题,即使经过几个小时的尝试

这是我的 ajax:

$.ajax({
type: 'POST',
url: 'scripts/manageHomePage.php',
//datatype: 'json',
data: dataString,
cache: false,
success: function(response) {
    //$.parseJSON(response);
    alert('response: ' + response);
    //alert('Got a successful return: ' + response.result);
    //window.location.href="documents/" + response.result;
},
error: function(response) {
    alert ('There was an error creating the archive: ' + response);
}
});

如您所见,我也一直在尝试使用 json 来执行此操作。下面是返回数据的 php 代码:

if ($zipArchive) {
$log->lwrite('passing to ajax function: ' . $archiveName);
//$return['error'] = true;
//$return['result'] = $archiveName;
return $archiveName;
} else {
$log->lwrite('Error creating zip file - not passed to form');
//$return['error'] = false;
//$return['result'] = 'There was an error creating the zip file';
return 'error';
}

再次,对 json 的更多尝试。无论如何,php 脚本返回的变量$archiveName是正确的。我在 $.ajax 的成功参数中的 js 警报显示"响应:"

我做错了什么?

您需要echo返回来自该函数的响应,而不是return它。