繁体   English   中英

jQuery,ajax,json,php解析器错误

[英]jQuery, ajax, json, php parsererror

我正在尝试运行一个Ajax请求,但遇到了parsererror。 这是我的Javascript代码:

$.ajax({
            type: 'POST',
            url: 'insertuser.php',
            dataType: 'json',
            data: {
                nickname: $('input[name=nickname]').val(),
                passwort: $('input[name=passwort]').val()
            },
            success : function(data){
                console.log(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                console.log("XMLHttpRequest", XMLHttpRequest);
                console.log("textStatus", textStatus);
                console.log("errorThrown", errorThrown);    
            }
        });

多数民众赞成在php文件:

$ return ['error'] = false;
$ return ['msg'] =“ juhuuu”;

回声json_encode($ return);

这是console.log:

XMLHttpRequest对象textStatus parsererror错误抛出语法错误

那就是.php的回应:{“错误”:false,“ msg”:“ juhuuu”}

我希望有人有一个主意:)

这个答案可能对别人没有帮助。 但是我在jQuery ajax json解析错误中遇到了类似的问题。 我的表单不会发送电子邮件,而是通过jQuery返回解析错误。

jQuery的

 $.ajax({
    type: "POST",
    url: "process-form.php",
    data: dataString,
    dataType:"json",
    success: function(response) {

        if(response.status == "success") {

        } else if (response.status == "error") {

        }
  });

的PHP

if (empty($name) || empty($email) || empty($phone) || !preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email) || !preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
            echo json_encode(array(
                'status' => 'error'
                //'message'=> 'error message'
            ));
        } else {
            $send = mail($sendTo, $subject, $message5, $headers, '‑fexample@example.com');
            $sendText = mail($sendToPhone, '', $txtmsg, 'From: example <noreply@example.com>\r\n');

            // insert into db
            $formName = $_POST['name'];
            $queryIn = "INSERT INTO database pseudo code";

            //execute the query. 
            $result = mysqli_query($connection, $queryIn);
            if (!$result) {
                printf("Error: %s\n", mysqli_error($connection));
                exit();
            }

        }
if($send){
            echo json_encode(array(
                'status' => 'success'
                //This is the message the .ajax() call was looking for but it never posted because there was a MySQL error being printed to the browser along with it. 
            ));
}

我的问题是ajax调用要到的PHP页面( process-form.php )。 我通过PHP mail()函数通过电子邮件发送表单数据,并将Web表单数据插入MySQL数据库。 我的PHP检查mail()发送成功,并将成功消息打印到屏幕上(这是jQuery .ajax()要查找的内容。但是我的php/mysql抛出错误并在屏幕上而不是在屏幕上显示错误因此,我的jQuery .ajax()调用试图读取一个PHP json_encode()编码的数组,但它得到的是MySQL Error一旦我修复了MySQL错误,一切都将正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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