0

I'm getting this error in my console and I've searched on google and here but I can't seem to find an answer that works.

It's supposed to be a chat. This fragment is responsable for sending the messages to database

Here's everything relevant in the console:

parsererror
SyntaxError: Unexpected end of JSON input
    at parse (<anonymous>)
    at jquery-3.5.1.min.js:2
    at l (jquery-3.5.1.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.min.js:2)

undefined // from console.warn(jqXHR.responseText)
error   @   script.js:84
c   @   jquery-3.5.1.min.js:2
fireWith    @   jquery-3.5.1.min.js:2
l   @   jquery-3.5.1.min.js:2
(anonymous) @   jquery-3.5.1.min.js:2
load (async)        
send    @   jquery-3.5.1.min.js:2
ajax    @   jquery-3.5.1.min.js:2
send    @   script.js:67

In the Network tab it shows taht there's no response

relevant JS code:

function send() {
    var text = $("#message").val();
    $("#message").val("");
    $.ajax({
        url:'Send.php',
        method: 'POST',
        dataType: 'json',
        data: { 'm': text },
        contentType: 'application/json',
        success: function () {
            console.log("succ");
            if (data.success) {
                ostatnia = data.Id;
                console.log(data);
            }
        },
        error: function (data, status, jqXHR) {
            console.log(data);
            console.log(status);
            console.log(jqXHR);
            console.warn(jqXHR.responseText);
        }
    });
}

Send.php

ob_start();
header('Content-Type: application/json');
include('server.php');//has db connection and starts session
    $m = new Message();
    $data = 0;
if(isset($_POST['m'])){

    
    if ($GLOBALS['conn']->connect_error) {
        $m->No($GLOBALS['conn']->connect_error);
    }
    else{
        $m->newMessage( $sesia, date("H:i:s d.m.y"), $_POST['m'], $GLOBALS['conn']);
        $sql = "INSERT INTO `chat`(`Id`, `Session_Id`, `Time`, `Content`, `Success`) VALUES (".$m->Id.",'".$m->Session_Id."','".$m->Time."','".$m->Content."',true)";//should work, I checked in phpMyAdmin
        if(!$GLOBALS['conn']->query($sql))
        {
            $m->No($GLOBALS['conn']->connect_error);
        }
        else{
        $m->weDidIt();
        }
    }
     $data = $m->returnMessage();
}

   
    return json_encode($data);

chat.php(the main page)

<script src="//code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="script.js"></script>

<style>
//(...)
</style>

<div id="box" name="box" style="border: 2px solid black; background-color: azure; height: 50%; width: 50%; margin: 5% 25%; ">
<div id="czat" name="czat" >
</div>
    </div>
    <input type="text" id="message" name="message" style="margin-left:25%; width:40%"/>
    <input type="submit" id="wyslij" style="width:10%;"/>

JaegerAurora
  • 1
  • 1
  • 1

1 Answers1

0

try to add in ajax property async: false, for waiting backend process(php) before use data What does "async: false" do in jQuery.ajax()?