0

Hello,

I have a small problem with my script. The ajax calls php file but it wont finsih successfully. Instead it goes to error: callback.

My javascript part:

function updateUserlist(){
        $.ajax({
            type: "POST",
            url: "process.php",
            data: {"function": "userList", "n_users": n_users},
            dataType: "json",
            success: function(data){
                console.log("Updating...");
                $("#users-list ul").append("<li>"+data.text['nickname']+"</li>");
                n_users = data.numUsers;
            },
            error: function(){
                console.log("Error with updating");
            }
        });
    }

And my PHP side:

$function = $_REQUEST['function'];

    $log = array();
    switch($function){
    case('userList'):
            /*$room_id = $_SESSION['room'];
            $n_users = $_REQUEST['n-users'];
            $connect->query("SELECT nickname FROM user_in_room, user WHERE user.id = user_in_room.user_id AND room_id = '$room_id'");
            $results = $connect->get();
            if($n_users == count($results)){
                $log['text'] = false;
            }else{
                $text = array();
                $log['numUsers'] = count($results);
                foreach($results as $num => $result){
                    if($num > count($results)){
                        $text = $result;
                    }
                }*/
                //$log['text'] = $text;
                $log['t'] = "asd";
            }
            break;
    }
    echo json_encode($log);

I commented the function pretty much out, thinking that It should be successfull then, but it doesnt return to success. Whats it wrong with the code ?

3
Contributors
7
Replies
33
Views
4 Years
Discussion Span
Last Post by Martin C++
0

If you navigate to process.php, what do you see? Also, for the error function, add a parameter (data) to it so that you can see what error is coming back.

0

Also, for the error function, add a parameter (data) to it so that you can see what error is coming back.

What stbuchok says :). Usually when I get an error it's because the specified file cannot be found. Javascript does not always use the same working directory as PHP, so make sure you specify the exact location to the file you want to load from the page you want to load it from. E.g. if you're making an AJAX call from index.php, and the file you're requesting is in a folder, you should include the folders in your "url" parameter.

0

The above php script is my process.php (A part of it, there are other cases too).

Minitauros that is not a problem. Because I have already determined that the function is called, it's just that the ajax doesnt work. I have an analog function and this one work just fine. I have compared them and they are almost the same. I cant figure out why my ajax isnt succeeding.

0

In your error callback you have no parameter. This parameter is what would house the error message that you could look at to determine what the issue is. This is why I said: "Also, for the error function, add a parameter (data) to it so that you can see what error is coming back."

So, please, add this and tell us what the error message is that is coming back.

function updateUserlist(){
        $.ajax({
            type: "POST",
            url: "process.php",
            data: {"function": "userList", "n_users": n_users},
            dataType: "json",
            success: function(data){
                console.log("Updating...");
                $("#users-list ul").append("<li>"+data.text['nickname']+"</li>");
                n_users = data.numUsers;
            },
            //notice the addition of the data parameter
            error: function(data){
                console.log("Error with updating");
            }
        });
    }

Use the developer toolbar of the browser you are using to step through the javascript as it executes to determine what the error message is that is coming back.

0

I fixed it. The problem with was that when I commented out the code, I had not comment the extra bracket out, so thats why I didnt get success in my "debug mode". And the reason that the script was initially not working was the fact that I sent the script $_POST['n_users'] but the script waited $_POST['n-users']. So now it's working. Thanks for help though.

PS. I didnt get the use of adding data to error callback. It didnt show any errors. If it's not too much trouble then maybe you could explain it to me.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.