Jump to content

unexpected end of json input


alidayan

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);
?>

 

Link to comment
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!" }

 

Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.