SyntaxError:意外的JSON输入结束 [英] SyntaxError: Unexpected end of JSON input

查看:318
本文介绍了SyntaxError:意外的JSON输入结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我的db中记录数据,但是ajax加载并不介入成功方法。问题是什么?

  error:SyntaxError:JSON输入的意外结束
Object.parse(native)
at n.parseJSON

record_data.php

 <?php 
$ type = getPar_empty(type,$ _ GET,);
$ new_nome = $ _ GET ['new_nome'];
$ new_cognome = $ _ GET ['new_cognome'];
$ new_email = $ _ GET ['new_email'];
$ new_lingua = $ _ GET ['new_lingua'];
if($ type ==add_user){
$ stmt = null; ('$ new $'','$ new_nome','$ new $','$'$'$'$' $ new_cognome, '$ new_lingua', '手动',现在(),0));
if(!$ stmt){
log_msg($ db-> error);
die();
}
$ stmt-> execute();
$ stmt-> close();
$ db-> close();
}
?>

脚本
< $($#$)$($#$)$($#$)$($#$)$ = $('#cognome')。val();
var email = $('#email')。val();
var lingua = $('#lingua')。val() ;
var dataString ='nome ='+ nome +'& cognome ='+ cognome +'& email ='+ email +'& lingua ='+ lingua +'& type = add_user';
$ .ajax({
类型:'GET',
data:dataString,
url:record_data.php,
成功:函数(结果){
$(#status_text)。html(result);
$('#nome')。val('');
$('#cognome')。val('');
$('#email')。val('');
},
错误:function(xhr,status,error){
console.log(error);
}
});
});

$('#adduser')。submit(function(){
return false;
});

表格

 < form name =adduserid =addusermethod =GETaction =#> 
< div class =col-md-3>
< div class =form-group m-b-30>
< p>电子邮件< / p>
< input class =form-controltype =emailid =emailname =emailplaceholder =indirizzo e-mailemail required>
< / div>
< / div>
< div class =col-md-3>
< div class =form-group m-b-30>
< p> Nome< / p>
< input class =form-controltype =textid =nomename =nomeplaceholder =nome>
< / div>
< / div>
< div class =col-md-3>
< div class =form-group m-b-30>
< p> Cognome< / p>
< input class =form-controltype =textid =cognomename =cognomeplaceholder =cognome>
< / div>
< / div>
< div class =col-md-3>
< div class =form-group m-b-30>
< p> Lingua< / p>
< select class =form-controlid =linguaname =lingua>
< option value =it> IT< / option>
< option value =en> EN< / option>
< / select>
< / div>
< input type =submitclass =btn btn-embossed btn-primary m-r-20id =salvaBtnvalue =Aggiungi utente>< / input>
< div id =status_text/>< / div>
< / div>
< / form>


解决方案

您的jquery ajax调用应该包含



dataType:'html',



明确声明数据从服务器返回的是 html



实际上您正在使用 result 和将它直接放入ID为 statusText






的DOM节点中您必须解决一个主要问题:您的PHP脚本未返回 echo ing)任何数据!请注意:真的应该使用 mysqli / strong>为您的数据库查询 http://php.net/manual/en/book。 mysqli.php ,并使用预处理语句来建立查询(而不是通过连接字符串来建立SQL查询)。

I can record data in my db but ajax loading doesn't inter in success method. What's the problem?

error: "SyntaxError: Unexpected end of JSON input
    at Object.parse (native)
    at n.parseJSON "

record_data.php

<?php
$type=getPar_empty("type",$_GET,"");
$new_nome=$_GET['new_nome'];
$new_cognome=$_GET['new_cognome'];
$new_email=$_GET['new_email'];
$new_lingua=$_GET['new_lingua'];
if ($type=="add_user"){
  $stmt=null;
  $stmt=$db->prepare("INSERT INTO newsletter_utenti(email,abilitato,nome,cognome,lingua,lista,data_creazione,unsubscribe) VALUES('$new_email',1,'$new_nome','$new_cognome','$new_lingua','manual',now(),0)");
  if (!$stmt) {
    log_msg($db->error);
    die();
  }
  $stmt->execute();
  $stmt->close();
  $db->close();
}
?>

script

   $("#salvaBtn").click(function(){
  var nome = $('#nome').val();
  var cognome = $('#cognome').val();
  var email = $('#email').val();
  var lingua = $('#lingua').val();
  var dataString = 'nome='+nome+'&cognome='+cognome+'&email='+email+'&lingua='+lingua+'&type=add_user';
  $.ajax({
    type:'GET',
    data:dataString,
    url:"record_data.php",
    success:function(result) {
              $("#status_text").html(result);
              $('#nome').val('');
              $('#cognome').val('');
              $('#email').val('');
    },
    error:function(xhr,status,error) {
      console.log(error);
    }
  });
});

$('#adduser').submit(function (){
  return false;
});

form

<form name="adduser" id="adduser" method="GET" action="#">
            <div class="col-md-3">
              <div class="form-group m-b-30">
                <p>E-mail</p>
                <input class="form-control" type="email" id="email" name="email" placeholder="indirizzo e-mail" email required>
              </div>
            </div>
            <div class="col-md-3">
              <div class="form-group m-b-30">
                <p>Nome</p>
                <input class="form-control" type="text" id="nome" name="nome" placeholder="nome">
              </div>
            </div>
            <div class="col-md-3">
              <div class="form-group m-b-30">
                <p>Cognome</p>
                <input class="form-control" type="text" id="cognome" name="cognome" placeholder="cognome">
              </div>
            </div>
            <div class="col-md-3">
              <div class="form-group m-b-30">
                <p>Lingua</p>
                <select class="form-control" id="lingua" name="lingua">
                  <option value="it">IT</option>
                  <option value="en">EN</option>
                </select>
              </div>
              <input type="submit" class="btn btn-embossed btn-primary m-r-20" id="salvaBtn" value="Aggiungi utente"></input>
              <div id="status_text" /></div>
            </div>
          </form>

解决方案

your jquery ajax call should include

dataType: 'html',

to explicitly state that the data coming back from the server is html

in fact you're taking result and putting it straight into the DOM node with id statusText


Then you have to fix a major issue: your PHP script is not returning (echoing) any data !


Bottom note: you really should use mysqli for your db queries http://php.net/manual/en/book.mysqli.php and also use prepared statements to build up the query (instead building up the SQL query by concatenating strings).

这篇关于SyntaxError:意外的JSON输入结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆