alidayan Posted May 2, 2020 Share Posted May 2, 2020 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/ Share on other sites More sharing options...
gw1500se Posted May 2, 2020 Share Posted May 2, 2020 Did you var_dump the Json string before decoding it to make sure it is valid? Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577446 Share on other sites More sharing options...
alidayan Posted May 3, 2020 Author Share Posted May 3, 2020 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!" } Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577468 Share on other sites More sharing options...
gw1500se Posted May 3, 2020 Share Posted May 3, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577473 Share on other sites More sharing options...
alidayan Posted May 3, 2020 Author Share Posted May 3, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577475 Share on other sites More sharing options...
maxxd Posted May 3, 2020 Share Posted May 3, 2020 You're checking $_GET, but using POST in your ajax call. Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577477 Share on other sites More sharing options...
alidayan Posted May 3, 2020 Author Share Posted May 3, 2020 15 minutes ago, maxxd said: You're checking $_GET, but using POST in your ajax call. So what to do? Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577478 Share on other sites More sharing options...
maxxd Posted May 3, 2020 Share Posted May 3, 2020 2 minutes ago, alidayan said: So what to do? Either change the AJAX method or the PHP method. They have to match. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577480 Share on other sites More sharing options...
alidayan Posted May 3, 2020 Author Share Posted May 3, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577482 Share on other sites More sharing options...
gw1500se Posted May 3, 2020 Share Posted May 3, 2020 (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 May 3, 2020 by gw1500se 1 Quote Link to comment https://forums.phpfreaks.com/topic/310678-unexpected-end-of-json-input/#findComment-1577484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.