简体   繁体   中英

AJAX request doesn't seem to recieve the response

I have the following code: AJAX:

 $.ajax({
                url: "mail.php",
                type: "POST",
                data: {first_name:first_name,last_name:last_name,email: email,telephone:telephone,message:message},
                cache: 'false',
                dataType: "json",
                success: function(response) {
                    {
                        if(response.status==1)
                            alert('Email sent!');
                        else
                            alert('Error.')
                    }
                }
            });

PHP:

header('Content-type: application/json');
$to="me@example.com";
$message=$_POST['message']."\r\n"."\r\n".$_POST['first_name'].' '.$_POST['last_name']."\r\n".$_POST['telephone'];
$subject="New Message!";
$email=$_POST['email'];
$headers="From:".$email;
$success=mail($to,$subject,$message,$headers);
if($success)
{
    $done=array("status"=>true);
    echo json_encode($done);
}
else
{
    $done=array("status"=>false);
    echo json_encode($done);
}

In the network panel, it seems like the response is not recieved and the type is "Pending"

If I am not mistaken, this

 success: function(response) {
                {
                    if(response.status==1)
                        alert('Email sent!');
                    else
                        alert('Error.')
                }
            }

should be

  success: function(response) {
                    if(response.status==1)
                        alert('Email sent!');
                    else
                        alert('Error.');
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM