【问题标题】:jQuery, ajax, json, php parsererrorjQuery、ajax、json、php 解析器错误
【发布时间】:2012-10-13 03:54:56
【问题描述】:

我试图让一个 ajax 请求运行,但我得到一个解析器错误。 这是我的 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);

这是控制台日志:

XMLHttpRequest 对象 textStatus 解析器错误 errorThrown SyntaxError

这就是 .php 的回声: {"error":false,"msg":"juhuuu"}

我希望有人有一个想法:)

【问题讨论】:

  • 通过var str = $(this).serialize();在php中发送数据parse_str($_POST["data"],$array);
  • 您是否尝试在 PHP 中添加内容类型标头,例如:header('Content-Type: application/json');
  • dataType jsonp 和标题的东西不起作用..
  • 好的,我修好了! :) 我只需要删除我的 php 文件中的所有 HTML 内容,如 、、
    等等……现在它可以工作了!

标签: php javascript jquery ajax parse-error


【解决方案1】:

这个答案可能会或可能不会帮助其他人。但是我遇到了一个与 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. 
            ));
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    相关资源
    最近更新 更多