I am firing an ajax post to a server side php file.
The php returns an "ok" when succeed or error 404 when error.
This is the ajax method
var formData = {
name: $('#name').val(),
email: $('#email').val()
};
// Stop the form actually posting
// Send the request
$.ajax({
url: "myserver/register.php",
type: "Post",
data: formData,
success: function () {
alert("success");
},
error: function(xhr, status, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
For some reason the call always return an error. In the php the data received and everything works fine but it keep and going to the error .
Here is my php code:
<?php
//return error if fields are not set
if (!isset($_POST['name']) || !isset($_POST['email']))
{
header('HTTP/1.0 404 Not Found');
http_response_code(404);
exit();
}
$from = "New Register <new_comment@user-app.com>";
$to = "new_register@user-app.com";
$subject = "New user just registered user!";
$name = $_POST['name'];
$email = $_POST['email'];
$message = "Name:" . $name . "\n" . "Email:" . $email ."\n";
mail($to,$subject,$message,"From: " . $from);
http_response_code(200);
echo "ok";
?>