marky marky - 1 year ago 66
Ajax Question

Having trouble returning php data to jQuery ajax call

It seems this type of question has been asked and answered a number of time before, but I just can't seem to get this working, even after hours of trying different things.

Here's my ajax:

type: 'POST',
url: 'scripts/manageHomePage.php',
//datatype: 'json',
data: dataString,
cache: false,
success: function(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);

As you can see I've also been trying to use json to do this. Here's the php code that returns the data:

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';

Again, more attempts at json. Anyway, the variable $archiveName that the php script is returning is correct. My js alert in the success parameter of $.ajax shows "response: "

What am I doing wrong?

Answer Source

You need to echo back a response from that function, not return it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download