Hello! When receiving data, ajax pops up a window with the error:

Imgur

JS part:

$('#btn').click(function(){ $comment = $('#comment').val(); $id = $('#comment').attr('name'); if($comment!=''){ $.ajax({ type: 'post', url:'file', data:{comment:$comment, id:$id}, dataType:'json', error: function(text, error, key, value) { alert('Ошибка AJAX: ' + text + ' | ' + error +' | '+ key, + value); }, success: function(data){ $('#test').prepend(data.author + data.comment + data.date); } }); $('#comment').val(''); $('.so-hard').removeClass('active'); } }); 

php part:

 if(isset($_POST['comment']) && isset($_POST['id'])){ $text = htmlspecialchars($this->input->post('comment')); $id = $this->input->post('id'); $author = 'Test'; $date = date('dmY H:i'); $this->data_m->add_comment($id,$author,$text,$date); echo json_encode(array('author'=>$author, 'comment'=>$text, 'date'=>$date)); } 

Yesterday everything worked fine, today a bug began to pop up, as far as I remember, I did not change anything in the code! Data is added to the database normally, but the data cannot be inserted into the page, apparently due to this error. In Firefox in a pop-up window it gives out

AJAX Error: [object Object] | parsererror | SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data.

I looked at the response from the server, such is the line at the very top of the page: {"author":"Test","text":"hfghfgh","date":"18-01-2014 23:29"}

  • one
    you get html instead of json, most likely this is a 500 error, see in the debugger what exactly comes to you through the request. - zb '
  • Alexey123, that's what I received: hostingkartinok.com/… - sew810i9
  • Already clearer. The file 'file' accessed by AJAX does not stop executing on JSON data output. Apparently, below the php code above, there is some other code that also sends almost a complete page of the site, which can be seen on your screenshot after JSON data. - Alex Krass
  • Alexey, here's the code 'file' pastebin.com/utma1Wj1 now the code for adding data goes to the bottom of the file, and before that the output was displayed at the bottom! But the coming result has not changed! Also the whole page pops up - sew810i9
  • one
    Yes I understand. In frameworks, splitting is usually done according to the type of content that should be sent back. You can search this topic for requests "codeigniter ajax pagination", that would not do another method of the controller. And about the source code - this is done on purpose and this cannot be undone. It is necessary that: 1. the user could see what came to him initially without modifications; 2. save the original page in its original form, not warped by the script and start from the beginning. 3. If the already modified code is interesting, its DOM tree can be viewed in the browser consoles. - Alex Krass

1 answer 1

In general, this error indicates that the response comes with a string with an unacceptable syntax for JSON, that is, some "<" is encountered and can be not seen when added to the page itself, if it is a tag. To make sure the answer is correct, do this: in javascript, remove the string

 dataType:'json', 

and add the output to the success function:

 success: function(data){ alert(data); } 

and see what will be displayed.