doutuan6158 2014-08-25 13:26
浏览 23

Ajax发布到php错误

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 + "
status: " + 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 . "
" . "Email:" . $email ."
";

mail($to,$subject,$message,"From: " . $from);

http_response_code(200);
echo "ok";
?>
  • 写回答

1条回答 默认 最新

  • duanlie3187 2014-08-25 13:32
    关注

    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 + "
    status: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            }
    });
    

    on your php get data using post:-

    $_POST['name']
    $_POST['email']
    
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图