Jump to content
alidayan

unexpected end of json input

Recommended Posts

Hi,

Ajax POST requests returns 200 OK, but fires error. I couldn't figure out why. Could anybody help me?

Javascript:

    function login(){
        $.ajax(
        {
            type: "POST",
            url: './api/login.php',
            dataType: 'json',
            data: {
                username: $("#username").val(),
                password: $("#pass").val()
            },
            success: function (result) {
                if (result['status'] == true) {
                    window.location.href = './welcome.php';
                }
                else {
                    alert(result['message']);
                }
            },
            error: function(xhr, status, error) {
            	if (xhr.responseText) {
            		oResponse = JSON.parse(xhr.responseText);
            		alert(oResponse.error.message)
            	} else {
            	    alert("Status: "+status+"\nXhr: "+JSON.stringify(xhr)+"\n\nError: " + JSON.stringify(error)); 
                }
            }
        });
    }

PHP:

<?php

    include_once '../config/database.php';
    include_once '../object/acc.php';
     

    $database = new Database();
    $db = $database->getConnection();

    $user = new acc($db);
    
    $user->username = isset($_GET['name']) ? $_GET['name'] : die();
    $user->password = isset($_GET['pass']) ? $_GET['pass'] : die();
    
    $stmt = $user->login();
    if($stmt->rowCount() > 0){
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if(password_verify($user->password, $row['pass'])){
            http_response_code(200);
            // create array
            $arr = array(
                "status" => true,
                "message" => "Successfully Login!",
                "id" => $row['id'],
                "username" => $row['name'],
                "email" => $row['email']
            );
        }
        else{
            http_response_code(401);
            $arr = array(
                "status" => false,
                "message" => "Invalid Username or Password!"
            );
        }
    }
    else{
        http_response_code(401);
        $arr = array(
            "status" => false,
            "message" => "Invalid Username or Password!"
        );
    }
    echo json_encode($arr);
?>

 

Share this post


Link to post
Share on other sites

Did you var_dump the Json string before decoding it to make sure it is valid?

Share this post


Link to post
Share on other sites

Sorry for late response. Yes I did. I got:

array(5) { ["status"]=> bool(true) ["message"]=> string(19) "Successfully Login!" ["id"]=> string(1) "6" ["username"]=> string(8) "alidayan" ["email"]=> string(20) "ali***@gmail.com" }

And if send wrong password/username I got:

array(2) { ["status"]=> bool(false) ["message"]=> string(29) "Invalid Username or Password!" }

 

Share this post


Link to post
Share on other sites

I guess you need to be more explicit. I was assuming the exception was thrown by the Json_decode. post the exact error message and indicate to which line it refers.

Share this post


Link to post
Share on other sites

When I run login.php?username=alidayan93@gmail.com&password=12345678

I got: {"status":true,"message":"Successfully Login!","id":"6","username":"alidayan","email":"alidayan93@gmail.com"} as response

If I call api/user/login.php from another page called login.php as well I got error which is attached

Ekran Resmi 2020-05-03 17.38.55.png

Share this post


Link to post
Share on other sites

You're checking $_GET, but using POST in your ajax call.

Share this post


Link to post
Share on other sites
15 minutes ago, maxxd said:

You're checking $_GET, but using POST in your ajax call.

So what to do?

Share this post


Link to post
Share on other sites
2 minutes ago, alidayan said:

So what to do?

Either change the AJAX method or the PHP method. They have to match.

  • Thanks 1

Share this post


Link to post
Share on other sites
18 minutes ago, maxxd said:

Either change the AJAX method or the PHP method. They have to match.

Thank you I had changed Post to Get from php that didn't worked but now I changed POST from AJAX to GET that worked.
Thank you so much.

Share this post


Link to post
Share on other sites
Posted (edited)

I'm guessing when you changed to POST you did not change the array to $_POST which is why it didn't work.

Edited by gw1500se
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.