0

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";
?>
Aviv Paz
  • 1,051
  • 3
  • 13
  • 28

1 Answers1

0

try type: "post",

$.ajax({
        url: "myserver/register.php",  //or full url http://ex.com/myserver/register.php
        type: "post",
        data: {'name': $('#name').val(),'email': $('#email').val()},
        success: function (data) {
            alert("success");
        },
        error: function(xhr, status, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        }
});

on your php get data using post:-

$_POST['name']
$_POST['email']
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • In the php file it activate the code perfectly, just keep going to the error section in the post method in the javascript – Aviv Paz Aug 25 '14 at 13:44