0

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?

Cbas
  • 6,003
  • 11
  • 56
  • 87
  • 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));` – Sam May 30 '14 at 21:09
  • possible duplicate of [Why do I get an extra element at the end in JSON?](http://stackoverflow.com/questions/7535724/why-do-i-get-an-extra-element-at-the-end-in-json) – Giacomo1968 May 30 '14 at 21:20
  • 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. – Cbas May 30 '14 at 21:22
  • @Sam So by your logic the code should just work with a `die()` or `exit()` at the end of the `expire_user` function? – Giacomo1968 May 30 '14 at 21:25
  • 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. – Sam May 30 '14 at 21:28
  • @Sam There might be another `echo` in there somewhere that is mucking up the works. – Giacomo1968 May 30 '14 at 21:31
  • @JakeGould according to OP's comment on my answer that was the case. – Sam May 30 '14 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. – FAQi Sep 15 '21 at 11:50

2 Answers2

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • neither of those edits changed the output. There is still an empty array on the return – Cbas May 30 '14 at 21:19
  • Check this thread. Could be a browser issue. JSON is inconsistently handled in some browsers. http://stackoverflow.com/questions/7535724/why-do-i-get-an-extra-element-at-the-end-in-json – Giacomo1968 May 30 '14 at 21:20
  • 1
    I didn't know about the headers though, that was very useful – Cbas May 30 '14 at 21:25
  • @Cbas Thanks! Even if my solution didn’t solve the issue, remember that lack of headers & browser inconsistencies can also cause oddities when you debug. So you need to iron out the basics until you find the thing that is choking the works. – Giacomo1968 May 30 '14 at 21:28
1

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.

Sam
  • 20,096
  • 2
  • 45
  • 71
  • 3
    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 – Cbas May 30 '14 at 21:31
  • 2
    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). – Sam May 30 '14 at 21:31
  • @Cbas “…but then I found an echo with an empty array somewhere towards the bottom when I checked again.” This is an excellent answer not because of it being production worthy or not but explains a debugging practice which is really helpful. +1 from me. – Giacomo1968 May 30 '14 at 21:34
  • 1
    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 – Sam May 30 '14 at 21:40