1

I'm writing an ajax application and have a function in a php script:

public function expire_user() {
    $r=array("return"=>'OK');
    echo json_encode($r); 
}

that gets called with this javascript:

$.getJSON("/users/expire_user",function(data){
        alert('success');
});

The problem is that the alert never displays. I think this is due to json_encode returning invalid json, because when I go to the url directly, it displays

{"return":"OK"}[]

which is not valid json due to the extra '[]' on the end. Why is json_encode putting the empty array on the end and how do I get rid of it so that I can receive valid json?

5
  • Can we see how expire_user is called from GET /users/expire_user? I bet it will work if you kill the script right away: die(json_encode($r)); Commented May 30, 2014 at 21:09
  • don't think its a duplicate because Sam's comment worked for me and that wasn't in the other answer. @Sam post your code as an answer and I'll accept it. Commented May 30, 2014 at 21:22
  • 2
    @JakeGould I was actually asking OP for more information, since the [] is returned by nothing in his shown code (i.e. what calls expire_user(). I was proving this by the die() example. I do not think this is a production-worthy use. Commented May 30, 2014 at 21:28
  • @JakeGould according to OP's comment on my answer that was the case. Commented May 30, 2014 at 21:32
  • The same thing happened to me, and I followed the execution sequence and my code was spitting an empty array in the next iteration of a loop when the input was empty. So I suspect the same could be in your case too, that somewhere your code is echoing empty array somewhere. Commented Sep 15, 2021 at 11:50

2 Answers 2

2

Wild guess, but maybe you should set correct headers for the JSON in your PHP function like this:

public function expire_user() {
    $r=array("return"=>'OK');
    header("Content-type: application/json");
    echo json_encode($r); 
}

Or actually send the content as X-JSON headers like this:

public function expire_user() {
    $r=array("return"=>'OK');
    $json_data = json_encode($r);
    header('X-JSON: (' . $json_data . ')');
    header('Content-type: application/x-json');
    echo $json_data;
}

A bit rusty on whether when using X-JSON the accompanying header should be application/x-json or just the normal application/json, but adding this caveat to help you debug.

Sign up to request clarification or add additional context in comments.

2 Comments

neither of those edits changed the output. There is still an empty array on the return
I didn't know about the headers though, that was very useful
2

This isn't quite an "answer", but I'm assuming your script is running some other code (maybe echo json_encode(array());) some time after expire_user() is called. To ensure that this is the last thing called you can use die() or exit():

public function expire_user() {
    $r = array("return"=>'OK');
    die(json_encode($r));
}

However, I suggest you try to debug the real problem. For instance, if you have a URL router than is handling requests and calling methods..it could be errantly echoing extra characters (that may cause more problems down the line). If you post your code that calls expire_user(), I can help debug further.


Disclaimer: I do not consider this a production-worthy solution. It needs more debugging, though.

3 Comments

Right again.. expire_users is actually a much longer function and I thought that those were the only two lines submitting output, but then I found an echo with an empty array somewhere towards the bottom when I checked again
Glad you were able to get to the bottom, I would not rely on die() then. It turns out to have been very helpful in debugging the problem though (var_dump(); and exit; are my friends).
Comprehending documentation and efficiently debugging have been the two most important skills over the years that didn't just "come" to me . Thanks for the +1 @JakeGould

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.