Javascript Ajax请求失败:JSON意外结束

Javascript Ajax请求失败:JSON意外结束,javascript,php,jquery,Javascript,Php,Jquery,我是JQuery的初学者,当我尝试将数据发布到我的PHP页面时,会出现以下错误: 结果:parsererror语法错误:JSON输入的意外结束200 ok 以下是我的php页面的代码: 我的check_和send_mail.php与另一个页面在同一个目录中,只有: 解决办法是什么 提前感谢您这部分代码: success : function(data){ var erreur = typeof(reponse.Error)!='undefined'

我是JQuery的初学者,当我尝试将数据发布到我的PHP页面时,会出现以下错误:

结果:parsererror语法错误:JSON输入的意外结束200 ok

以下是我的php页面的代码:

我的check_和send_mail.php与另一个页面在同一个目录中,只有:


解决办法是什么


提前感谢您

这部分代码:

success : function(data){
                      var erreur = typeof(reponse.Error)!='undefined' ?    reponse.Error : null;
                      var result = typeof(reponse.result)!='undefined' ? reponse.result : null;
                      var list_grp = $("#list_grp");

                      alert('it is okay');            
                },           
应该是这样的:

 success : function(reponse){
                      var erreur = typeof(reponse.Error)!='undefined' ?  reponse.Error : null;
                      var result = typeof(reponse.result)!='undefined' ? reponse.result : null;
                      var list_grp = $("#list_grp");

                      alert('it is okay');            
                },           
您收到的响应是“数据”变量,而不是您正在使用的“响应”变量。
注意:请尝试更正“response”的拼写错误,以确保将来不会让您感到困惑

我认为您不需要对数据进行JSON字符串化,只需更新mydata变量并删除JSON.stringify即可

var mydata = {'personName':name, 'subjectName':name, 'commentArea':comments};         
$.ajax({            
    type: "POST",             
    url: "check_and_send_mail.php",           
    data: mydata,
    dataType: "json",             
    success : function(data){
       // here data is a response
    },            
    error : function(xhr, status, error){
      // handle error
    }         
});
然后可以从PHP发送响应

 $response = true; // Just a true response for now
 // Send the response
 header('Access-Control-Allow-Origin:*');
 ob_clean();
 echo json_encode($response);
 die();

我已经更正了,但我总是收到相同的错误消息

$('#contactUsForm').on('submit',function(event){
    event.preventDefault();

    var name  = $("#name").val();
    var subject = $("#subject").val();
    var comments = $("#commentArea").val();

    var mydata = {personName:name, subjectName:name, commentArea:comments};
    $.ajax({ 
        type: "POST",
        url: "check_and_send_mail.php",
        data: JSON.stringify(mydata),
        dataType: "json",
        success : function(response){
            var erreur = typeof(response.Error)!='undefined' ? response.Error : null;
            var result = typeof(response.result)!='undefined' ? response.result : null;
            var list_grp = $("#list_grp");

            alert('it is okay');
        },
        error : function(xhr, status, error){
              alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
            //afficher_erreur('Ajax error :' + formatErrorMessage(jqXHR, textStatus));
        }
    });
});

我听从了你的建议,但不起作用:

$('contactUsForm')。在('submit',函数(事件){ event.preventDefault()


我已经按照你的建议尝试过了,但我总是收到相同的错误消息你是否也像我一样尝试过更新mydata变量?同时删除JSON.stringify。它在我这方面工作得非常好。在你的check和send.php文件中,除了我在上面的回答中添加的代码之外,不要添加任何东西来进行测试
 $response = true; // Just a true response for now
 // Send the response
 header('Access-Control-Allow-Origin:*');
 ob_clean();
 echo json_encode($response);
 die();
$('#contactUsForm').on('submit',function(event){
    event.preventDefault();

    var name  = $("#name").val();
    var subject = $("#subject").val();
    var comments = $("#commentArea").val();

    var mydata = {personName:name, subjectName:name, commentArea:comments};
    $.ajax({ 
        type: "POST",
        url: "check_and_send_mail.php",
        data: JSON.stringify(mydata),
        dataType: "json",
        success : function(response){
            var erreur = typeof(response.Error)!='undefined' ? response.Error : null;
            var result = typeof(response.result)!='undefined' ? response.result : null;
            var list_grp = $("#list_grp");

            alert('it is okay');
        },
        error : function(xhr, status, error){
              alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
            //afficher_erreur('Ajax error :' + formatErrorMessage(jqXHR, textStatus));
        }
    });
});
    var name  = $("#name").val();
    var subject = $("#subject").val();
    var comments = $("#commentArea").val();

    var mydata = {'personName':name, 'subjectName':name, 'commentArea':comments};
    $.ajax({ 
        type: "POST",
        url: "check_and_send_mail.php",
        data: mydata,
        dataType: "json",
        success : function(response){
            var erreur = typeof(response.Error)!='undefined' ? response.Error : null;
            var result = typeof(response.result)!='undefined' ? response.result : null;
            var list_grp = $("#list_grp");

            alert('it is okay');
        },
        error : function(xhr, status, error){
              alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
            //afficher_erreur('Ajax error :' + formatErrorMessage(jqXHR, textStatus));
        }
    });
});