1

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:

$.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);
}
});

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?

4
  • try to console.log response instead of alerting to see if its coming through clean and populated. Commented Apr 11, 2012 at 1:17
  • Thanks for the quick reply! How do I do that? I'm using Aptana Eclipse for my IDE. Commented Apr 11, 2012 at 1:21
  • I figured out how to do it. I'm getting "undefined" in the console as well. Commented Apr 11, 2012 at 1:26
  • sorry i overlooked you were returning, not echoing your response on an ajax call, thats why you're getting undefined. Commented Apr 11, 2012 at 2:03

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great! That worked! But can someone explain to me why echo works, but return doesn't?
its because you need to output content on an ajax call, return in that instance just exits the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.