This is giving me headaches, AGAIN. I either don’t understand this ajax response stuff at all, or it’s poorly coded.
Let’s establish an extremely simple example to work with:
register.php:
<?php
echo 'I want some response !!';
?>
ajax call:
$.ajax({
url: '/register.php',
type: 'POST',
data: $('#form-registracia').serializeArray(),
success: function(e){
var response = e.responseText;
alert(response);
}
});
Alert says undefined. Why? I tried messing with it for hours now, I read the jquery site, nothing helps. I’m sure the PHP file gets executed and that the echo is sent back to ajax.
Thanks in advance!
you are getting undefined because the response returned from the server is contained within e, not e.responseText. responseText is a property of the XHR object, but jQuery encapsulates that for you and provides the responseText as the argument in the success method.
so in short, change it to this:
success: function(e){
var response = e;
alert(response);
}