我正在向服务器端php文件发射ajax post。
成功时返回"ok",错误时返回404。
这是ajax方法
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);
}
});
由于某种原因,调用总是返回错误。在php中,数据接收和一切工作正常,但它保持并走向错误。
下面是我的php代码:<?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";
?>
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);
}
});
在PHP中使用post:-
获取数据$_POST['name']
$_POST['email']