简体   繁体   English

PHP中的JSON输入Ajax意外结束

[英]Unexpected end of JSON input ajax in php

sorry for bad english writing 对不起,英语写作不好
i write some ajax code that chaining together. 我写了一些链接在一起的ajax代码。
first modal,when first done in success ,hide modal and secound show and send another ajax. 第一个模态,当第一次成功完成时,隐藏模态和secound并发送另一个ajax。
js file js文件

var timer = new Timer({
tick: 1,
ontick: function (s) {
    $('.timer').html(Math.round(s / 1000) - 1);
},
onstart: function () {
    $('.text-white').prop("disabled", true);
},
onstop: function () {
},
onpause: function () {
    console.log('timer set on pause')
},
onend: function () {
    $('.text-white').prop("disabled", false);
}
});

(function ($, timer) {

$('#sendCode').on("click", function () {

    var a = $.ajax({
        type: 'post',
        url: 'ajax-processor.php',
        data: {
            mobile: $('#agent_mobile').val(),
            actioncode: 23
        },
        dataType: "json",
        success: function (response) {

            switch (response.errorcode) {
                case 0://every things ok
                    timer.start(50);
                    $('.step1').hide();
                    $('.step2').show();
                    $('.verifySend').on('click', function () {
                        b = a.then(function (daat) {
                            return $.ajax({
                                type: 'post',
                                url: 'ajax-processor.php',
                                dataType: "json",
                                data: { mobile: daat.mobile , token: $('#verifycode-form-field').val() , actioncode: 24},
                                success: function (response2) {
                                    $('.msg').html(response2.strmsg).show();
                                    console.log(response2);
                                },
                                error: function (xhr, status, error) {
                                    console.log(status);
                                    console.log(error);    //verify.js:51
                                }
                            });
                        });
                    });
                    $('.changeNumber').on('click', function () {
                        $('.step1').show();
                        $('.msg').hide();
                        $('.step2').hide();
                    });
                    $('.text-white').on('click', function () {
                        timer.start(50);
                        $('.step1').show();
                        $('.msg').hide();
                        $('.step2').hide();
                    });
                    break;
                case 1://time less than 1min
                    $('.msg').html(response.strmsg).show();

                    break;
                case 2://mobile verify
                    $('.msg').html(response.strmsg).show();
                    break;
                case 3://wrong number
                    $('.msg').html(response.strmsg).show();
                    break;
                default:
                    console.log("contact us");
            }

        }
    });
});

})(jQuery, timer);

php file php文件

private function phoneVerify($mobile)
{
    header('Content-Type: application/json');
    global $mysqli;
    $update = 0;// 0 insert / 1 update
    $error = 0;//0 no error/ 1 mobile exist but time less than 1 / 2 mobile verify
    $pattern = preg_match("/^9[0-9]{9}$/", $mobile);
    $row = $mysqli->query("SELECT * FROM `bv_sms` WHERE `mobile`='" . $mobile . "'");
    if ($mobileEx = $row->fetch_assoc()) {

        if ($mobileEx['status'] == 0) {

            $now = new DateTime('now');
            $create_at = new DateTime($mobileEx['create_at']);
            $interval = $now->diff($create_at);
            if ($interval->i < 1) {
                $error = 1;
            }else {
                $update = 1;
            }
        } else {
            $error = 2;
        }
    }

    return array('error' => $error, 'pattern' => $pattern,'update' => $update);
}

public function sendVerifyCode()
{
    header('Content-Type: application/json');
    global $bsiCore;
    global $mysqli;
    $errorcode = 0;//0 no error/ 1 mobile exist but time less than 1/ 2 mobile vrify / 3 mobile number wrong
    $msg = "";
    $mobile = $bsiCore->ClearInput($_POST['mobile']);
    $mobileVer = $this->phoneVerify($mobile);
    if ($mobileVer['pattern'] == 1) {

        if ($mobileVer['error'] == 0) {
            $token = rand(10000, 99999);
            try {

                //@TODO set if result ok then send query to database
                if($mobileVer['update'] == 0){
                    $mysqli->query("INSERT INTO `bv_sms` (`mobile`,`token`) VALUES ('" . $mobile . "','" . $token . "')");
                }else {
                    $mysqli->query("UPDATE `bv_sms` SET `token`='" .$token. "' WHERE `mobile`='".$mobile."'");
                }

            } catch (ApiException $e) {
                //$msg = "به دلیل بروز خطا در ارسال لطفا یک دقیقه بعد تلاش کنید.";
            } catch (HttpException $e) {
                //$msg = "به دلیل بروز خطا در ارسال لطفا یک دقیقه بعد تلاش کنید.";
            }

        } else {
            if ($mobileVer['error'] == 1) {
                $msg = "باید از ارسال کد اول یک دقیقه گذشته باشد.";
                $errorcode = 1;
            } else {
                $msg = "این شماره قبلا تایید شده است.";
                $errorcode = 2;
            }
        }
    } else {
        $msg = "شماره ی وارد شده معتبر نمیباشد.";
        $errorcode = 3;

    }

    $strmsg = '<i style="color:#F00;">' . $msg . '</i>';
    echo json_encode(array("mobile" => $mobile, "errorcode" => $errorcode, "strmsg" => $strmsg));
}

private function abc($a,$b){//$a = phone number get whit $_post['mobile'] $b= token  get same
    header('Content-Type: application/json');
    global $bsiCore;
    global $mysqli;
    $mobile = $bsiCore->ClearInput(intval($a));
    $token = $bsiCore->ClearInput(intval($b));
    $row = $mysqli->query("SELECT FROM `bv_sms` AS `bs` WHERE `bs`.`mobile`='" .$mobile. "' AND `bs`.`token`='" .$token. "' AND `bs`.`create_at` < (NOW() - INTERVAL 1 MINUTE)");
    if($row->fetch_assoc()){
        $mysqli->query("UPDATE `bv_sms` AS `bs` SET `bs`.`status`=1 WHERE `bs`.`mobile`='" .$mobile. "' AND `bs`.`token`='" .$token. "'");
        return true;
    }
    return false;
}
public function checkVerifyCode($a,$b)
{
    header('Content-Type: application/json');

    $errorcode = 1;//0 no error/ 1
    $msg = "کد تایید نامعتبر است.";

    if($this->abc($a,$b)){
        $msg = "تایید شد.";
        $errorcode = 0;
    }


    $strmsg = '<i style="color:#F00;">' . $msg . '</i>';
    return json_encode(array("errorcode" => $errorcode, "strmsg" => $strmsg));

}

first ajax send successfully 第一个ajax发送成功
but secound ajax send as a json but response empty and this error log in console 但secound ajax作为json发送,但响应为空,并且此错误日志在控制台中

parsererror
SyntaxError: Unexpected end of JSON input    verify.js:51
at JSON.parse (<anonymous>)
at n.parseJSON (jquery-1.12.4.min.js:4)
at Xb (jquery-1.12.4.min.js:4)
at y (jquery-1.12.4.min.js:4)
at XMLHttpRequest.c (jquery-1.12.4.min.js:4)

i try and try and finaly find problem. 我尝试并最终找到问题。
first problem is in php file ,where i send query to database. 第一个问题是在php文件中,我将查询发送到数据库中。
query failed so $row is boolean and when $row->fetch_assoc() return fatal error and do this.problem is SELECT FROM instead SELECT * FROM 查询失败,因此$ row是布尔值,并且当$ row-> fetch_assoc()返回致命错误并执行此操作时。问题是SELECT FROM而不是SELECT * FROM
if in php file use var_dump or other debuging function you get error.because return non ajax response and make problem. 如果在php文件中使用var_dump或其他调试功能,则会收到错误。因为返回非ajax响应并产生问题。
and this page very help to me in find problem whit console.warn(xhr.responseText) 这个页面对我发现控制台console.warn(xhr.responseText)问题很有帮助
"SyntaxError: Unexpected token < in JSON at position 0" in React App sorry for bad writing 对不起,React App中的“ SyntaxError:JSON位置0中的意外令牌<不足”
be happy,omid 开心一点

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

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