AJAX第二次不会获得JSON数据

问题描述:

I'm sending Ajax request to a php file, that file returns an JSON array of the submitted data, the first time is great, it sends the data and gets it, so i can use it, but second time it gives me an error

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

My goal is to validate the data in the php file, if the data is not validated then i'm adding an error classes to the fields that are not validated, then the user correct those fields, and sends the form again, without refreshing.

My testing shows that its working for the first time, but when i want to send the form the second time, it gives me the above error, any clues what it can be?

AJAX :

$('#ajax').submit('submit', function(){

var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};

that.find('[name]').each(function(index, value){
    var that = $(this),
    name = that.attr('name'),
    value = that.val();

    data[name] = value;
});

$.ajax({
    url: url,
    type: type,
    data: data,
    cache:false,

    success: function(response){
        var json = response,
        obj = JSON.parse(json);
        if(obj.title == 'Mrs.'){
        //  $('#title').addClass('has-warning');
            $(function () {
              $('#name').tooltip({ 'title': 'Please fill name'});
            });
        }
        alert(obj.title);
    }

});
return false;
});

PHP file:

if(Input::exists()){
if(token::check(Input::get('token'))){

    echo json_encode(
    array(
        "title" => Input::get('title')
        )
    );
}

}

我正在向一个php文件发送Ajax请求,该文件返回提交数据的JSON数组,第一个 时间很棒,它发送数据并获取它,所以我可以使用它,但第二次它给我一个错误 p>

  SyntaxError:JSON.parse:意外的数据结束 在JSON数据的第1行第1行
  code>  pre> 
 
 

我的目标是验证php文件中的数据,如果数据未经过验证,那么我正在添加 错误类到未验证的字段,然后用户更正这些字段,然后再次发送表单,而不刷新。 p>

我的测试显示它第一次工作,但是当 我想第二次发送表单,它给我上面的错误,任何线索它可以是什么? p>

AJAX: p>

  $('#ajax')。submit('submit',function(){
 
var = $(this),
url = that.attr('action'),
type = that.attr(' 方法'),
data = {}; 
 
that.find('[name]')。each(function(index,value){
  var that = $(this),
 name = that.attr('name'),
 value = that.val(); 
 
 data [name] = value; 
}); 
 \  n $ .ajax({
 url:url,
 type:type,
 data:data,
 cache:false,
 
 success:function(response){
 var json = response,
  obj = JSON.parse(json); 
 if(obj.title =='Mrs。'){
 // $('#title')。addClass('has-warning'); 
 $(function  (){
 $('#name')。tooltip({'title':'Please fill name'}); 
}); 
} 
 alert(obj.title); 
} 
  
}); 
return false; 
}); 
  code>  pre> 
 
 

PHP文件: p>

  if(  Input :: exists()){
if(token :: check(Input :: get('token'))){
 
 echo json_encode(
 array(
“title”=> 输入:: get('title')
)
); 
} 
 
} 
   code>  pre> 
  div>

Your token check is the culprit here. It's not working the second time because it's returning an empty response. This is because you probably need to recreate a new Token and send it with the first response. To test if this is the issue, just write :

if(Input::exists()){
 if(token::check(Input::get('token'))){

  echo json_encode(
  array(
    "title" => Input::get('title')
    )
  );
 }else{
   echo json_encode(
     array("title" => 'See? You need to update token')
   );
 }
}

You should regenerate a new Token and then update it's value in the form :

echo json_encode(
     array("newToken" => Token::generate() )
   );