A
A
Anton Volkov2020-06-02 07:54:48
PHP
Anton Volkov, 2020-06-02 07:54:48

Uncaught SyntaxError: Unexpected end of JSON input?

I process requests through ajax.
All requests are passed, except those that are processed by the database.
Previously, there was a file connection error, I coped with it. Now the error is "Unexpected end of JSON input".
js file

$( document ).ready(function() {
    $("#do_signup").click(
    function(){
      sendAjaxForm('result_log', 'ajax_form', '../php_action/log.php');
      //sendAjaxForm('result_pas', 'ajax_form', '../php_action/pas.php', 'in_2');
      return false; 
    }
  );
});
 
function sendAjaxForm(result_form, ajax_form, url) {
    $.ajax({
        url:     url, //url страницы (action_ajax_form.php)
        type:     "POST", //метод отправки
        dataType: "html", //формат данных
        data: $("#"+ajax_form).serialize(),  // Сеарилизуем объект
        success: function(response) { //Данные отправлены успешно
        	result = $.parseJSON(response);//<------------ ЗДЕСЬ 35-ая строчка
        	$('#' + result_form).html(result.errors + "<script>border_mistake('in_1');</script>");
    	},
    	error: function(response) { // Данные не отправлены
            $('#' + result_form).html('Ошибка. Данные не отправлены.');
    	}
 	});
}

log.php file
<?php
  require "../db.php";

  $data = $_POST;
  $errors = array();

  if( trim($data['login']) == '' ):
  $errors['errors'] = 'Введите логин' ;

  elseif( R::count('users', 'login = ?', array($data['login'])) > 0 ):
  $errors['errors'] = 'Пользователь с таким логином уже существует';

  endif;

  if (!empty($errors)){
  echo json_encode($errors);
  }

?>

In the db.php plug-in file, the RedBeanPHP library is connected, with the help of which requests to the database are processed and the session is started.
Error from console
5ed5da0fe5759293775837.png
AND from network
This is processing request " if( trim($data['login']) == '' ): " in log.php (works fine)
5ed5da9fe73bc237906210.png
This is processing request "elseif( R::count('users ', 'login = ?', array($data['login'])) > 0 ): " in log.php (gives error in console)
5ed5daab33df1363358330.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AUser0, 2020-06-02
@istrebitel_lgg

According to the code, if the user exists - there will be an error, and there will be JSON with this error.
And if such a user does NOT exist, there will be an empty output, without any JSON at all, like you have in the second screenshot, right? So do something

else:
$errors['success']="Пользователь радостно создан. Аллилуя!";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question