Unexpected end of json input
I am making a request to a php file, and the php file should send back a json encoded array, but I'm getting unexpected end of json input for some reason. Here is the JS and the PHP:
23 Replies
you'll have to check the network request in the network tab of the devtools, it's almost always a PHP error when you get unexpected end of json data in javascript
you should be able to see the actual body it's sending back, and determine where the problem is
also, as a general tip, using numbers as a value that means something other than the number it represents is a bad practice.
$action
can just have a descriptive value, like update_user
and load_data
or whatever. It'll make your code infinitely more readable, cause right now I'd have to go find where $action
is set, figure out what the ternary expression is doing to $_POST['action']
, then remember what I think the value is going to be and find where I was looking at the code when I wondered wtf $action
was in the first placeAt the request response I dont see anything
then check your PHP error logs
you're also making account_functions.php do way to much. It logs people out, gives you lists of data, updates user information... that should really be three separate files, it makes debugging much easier
reading the code, I think maybe your request is failing at the
$verified
part, maybe try debugging around that, make sure it's actually passing verification?
Also, why are you storing the real name base64 encoded? It's returning the same error if I set the verified variable true
Error logs are disabled for my php configuration. how can I enable it?
hm, this should really already have taken care of showing the errors
what do you see when you open
account_functions.php?action=1
in your browser?nothing
an empty 200 response
I enabled error logs for php
is there anything in the logs?
I think I have to wait a couple of minutes to process the changes, beacuse I think my php files are not working now.
So its saying
I think the problem is with the session variables, and I don't know how, but another php file is having the same problem now.
The other error is
line 5:
that just means you're trying to access an array key that doesn't exist, it's not necessarily an error
those errors also aren't referencing the PHP file you're having problems with, right?
I didnt see these are different files, but they are
you'll probably have to start doing the janky PHP debugging thing where you add
die("got to here");
on lots of lines and then rerun the code until it doesn't print that outmy file name is account_functions.php
to be clear, put it in there once and move the statement down until you don't get it printing, then you know where things go wrong
yeah but I dont understand, beacuse these files worked before
that usually means it's related to state, so in this case your session
like, there's apparently a bug in your code that's triggered by not having a certain piece of state set, and because you had that piece of state set when you wrote it, it never occured. Now it's not set, and you are running into the bug
oh, I missed something btw, your code only runs in a POST (which you should only use to store data, generally not to request it), so just loading in the browser wouldn't work, sorry
So I cant get response from a post request?
ah, and also: You're posting to
?action=1
. That sets $_GET['action']
to 1, not $_POST['action']
you can, it's just bad practice to only fetch data from a POST request
the HTTP verbs have meaning, GET is to fetch data, POST is to send data, PUT and DELETE are to update and remove respectively
generally you only use GET and POST, where GET is for fetching and POST is for actions that cause changes (or in rare circumstances are expensive and really cannot be done by bots crawling your site)
This is probably actually the problem. $_POST['action']
isn't set. Try using $_REQUEST['action']
instead
(this is partially caused by trying to have one file do many things btw. Files are free, if you had a fetch_user_data.php
, you wouldn't have to check an action
parameter with unclear values and could just return data after your verification checks)Yeah, my JS file is the same. A lot of request functions to the server. I would probably need to make multiple files for each request.
By the way its not working with $_GET or $_REQUEST, too
I mean, you put the name of the file in the
fetch
, right?
then you'll have to do the die()
trick, I don't know what's wrong then without being able to run the code myself (and that's too much setup, I don't have time for that)yes I understand. I will try to fix the issues. Thanks for the tips.
if you find what line the code deviates from what you expect it to do, I'd be happy to help more
Thank you
I fixed the original issue, I switched to get method, the problem was the Header("Location: auth"), instead this I'm redirecting the user client side:
The auth file sets the session "account_verified" true if the user enters his password and sends back the user to the manage-account file where the user datas will be send for the user and the session "account_verified" will be set to false again.
The remain errors about I didnt use isset(), and I think the site is working again now
And the account managment done with verification