Javascript php中JSON输入ajax的意外结束
对不起,英文写得不好Javascript php中JSON输入ajax的意外结束,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,对不起,英文写得不好 我编写了一些链接在一起的ajax代码。 第一个模式,当第一次成功时,隐藏模式和secound show并发送另一个ajax。 js文件 var timer = new Timer({ tick: 1, ontick: function (s) { $('.timer').html(Math.round(s / 1000) - 1); }, onstart: function () { $('.text-white').prop("disabled", tru
我编写了一些链接在一起的ajax代码。
第一个模式,当第一次成功时,隐藏模式和secound show并发送另一个ajax。
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);
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));
}
php文件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);
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));
}
专用功能电话验证($mobile)
{
标题('Content-Type:application/json');
全球$mysqli;
$update=0;//0插入/1更新
$error=0;//0不存在错误/1移动设备,但时间少于1/2移动设备验证
$pattern=preg_match(“/^9[0-9]{9}$/”,$mobile);
$row=$mysqli->query(“从`bv_sms`中选择*,其中`mobile`=''.$mobile.”);
如果($mobileEx=$row->fetch_assoc()){
如果($mobileEx['status']==0){
$now=新日期时间(“now”);
$create_at=new DateTime($mobileEx['create_at']);
$interval=$now->diff($create_at);
如果($interval->i<1){
$error=1;
}否则{
$update=1;
}
}否则{
$error=2;
}
}
返回数组('error'=>$error,'pattern'=>$pattern,'update'=>$update);
}
公共函数sendVerifyCode()
{
标题('Content-Type:application/json');
全球$bsiCore;
全球$mysqli;
$errorcode=0;//0无错误/1手机存在,但时间少于1/2手机验证/3手机号码错误
$msg=”“;
$mobile=$bsiCore->ClearInput($_POST['mobile']);
$mobileVer=$this->phoneVerify($mobile);
如果($mobileVer['pattern']==1){
如果($mobileVer['error']==0){
$token=兰特(1000099999);
试一试{
//@TODO设置是否结果正常,然后将查询发送到数据库
如果($mobileVer['update']==0){
$mysqli->query(“插入到`bv_sms`(`mobile`、`token`)值('''$mobile.'''、'$token.'')中);
}否则{
$mysqli->query(“更新`bv_短信`SET`token`='”、$token.“,其中`mobile`='”、$mobile.”;
}
}捕获(APIE){
//msg;
}捕获(HttpException$e){
//msg;
}
}否则{
如果($mobileVer['error']==1){
msg;
$errorcode=1;
}否则{
$msg=“اینش㶈㶒㶈㶈㶈㶈㶈㶦㶈㶦㶈㶦。”;
$errorcode=2;
}
}
}否则{
$msg=“مایواششدا㶎㶎㶎㶎”;
$errorcode=3;
}
$strmsg='.$msg';
echo json_编码(数组(“mobile”=>$mobile,“errorcode”=>$errorcode,“strmsg”=>$strmsg));
}
私有函数abc($a,$b){/$a=电话号码获取和$u post['mobile']$b=令牌获取相同
标题('Content-Type:application/json');
全球$bsiCore;
全球$mysqli;
$mobile=$bsiCore->ClearInput(intval($a));
$token=$bsiCore->ClearInput(intval($b));
$row=$mysqli->query(“从`bv_sms`中选择`bs`,其中`bs`.`mobile`=''.$mobile.”和`bs`.`token`='.$token.”和`bs`.`create_at`<(现在()-间隔1分钟)”;
如果($row->fetch\u assoc()){
$mysqli->query(“将`bv_sms`更新为`bs`设置`bs`.`status`=1,其中`bs`.`mobile`=''.$mobile.”和`bs`.`token`='.$token.”);
返回true;
}
返回false;
}
公共函数校验码($a,$b)
{
标题('Content-Type:application/json');
$errorcode=1;//0无错误/1
$msg=“کدتییدنعت㶫تت”;
如果($this->abc($a,$b)){
$msg=“تاییدشد”;
$errorcode=0;
}
$strmsg='.$msg';
返回json_encode(数组(“errorcode”=>$errorcode,“strmsg”=>$strmsg));
}
第一次ajax发送成功但是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)
parserror
SyntaxError:JSON输入verify的意外结束。js:51
在JSON.parse()处
在n.parseJSON(jquery-1.12.4.min.js:4)
在Xb(jquery-1.12.4.min.js:4)
在y(jquery-1.12.4.min.js:4)
在XMLHttpRequest.c(jquery-1.12.4.min.js:4)
我试了又试,终于发现了问题。第一个问题是在php文件中,我将查询发送到数据库。
查询失败,因此$row是布尔值,当$row->fetch_assoc()返回致命错误并执行此操作时。问题是选择自,而不是选择自
如果在php文件中使用var_dump或其他调试函数,则会出现错误。因为返回非ajax响应并产生问题。
这个页面非常有助于我在console.warn(xhr.responseText)
React应用程序中的“SyntaxError:JSON中位置0处的意外标记<” 抱歉写得不好
很高兴,omid您不是以json形式返回响应,而是使用数组。使用
json\u encode()json@PankajMakwana哪个fanction bro?哪个函数用于响应?@PankajMakwana此函数用于响应(checkVerifyCode)和返回json_encode