Jquery json echo
EDIT Solved it. It was before the ajax call and, hence, this code. Thank you all for the answers.
I can't find anybody with this problem. I have a AJAX call to a PHP script that returns a JSON response.
I fetch the values from a database into an array:
while($row = mysql_fetch_array($ret)){
开发者_C百科 $temp = array(
'id' => $row['id_reserva'],
'start' => $row['data_inicio'],
'end' => $row['data_fim'],
'title' => $row['descricao']
);
$js[] = $temp;
}
Headers:
header('Content-type: application/json');
And echo:
echo json_encode($js);
There is no return whatsoever, just a null string. What is really bugging me is that if instead of this I create a json string with the previous result directly in the code:
$temp = '[{"id":"3914", "start": "2011-08-25 09:00:00",
"end":"2011-08-25 18:00:00", "title":"asdasdasd"},
{"id":"3915", "start": "2011-08-25 09:00:00",
"end":"2011-08-25 18:00:00", "title":"asdasdasd"}]';
echo $temp;
It works. Tried changing the file codification, comparing the strings, checking for some problem with chars, and nothing.
Anybody?
Cheers
you have to encode it. try
echo json_encode($js);
You need to json encode array:
echo json_encode($js);
You're not outputting as JSON?
echo json_encode($js);
With your method, when jQuery gets the response and cannot parse the JSON it will return the empty string as you have experienced.
Process of elimination..
- can you print_r ($js) and get output?
- Take out the header(); statement, does anything appear?
- Do you have error_reporting and display_errors turned on?
- You don't seem to initializing 'js' ($js = array()). If you get the literal string 'null' back, this could simply imply that your query is not returning any results.
I could add this statement to almost every long-winded PHP requestion on stack overflow:
Go through your code step by step and echo what you expect the contents of variables to be at that point. Then you'll slowly isolate exactly where your issue is; there's nothing wrong with your code as-is.
Are you not making a multi-dimentional array with $js[] = $temp;
and then encoding that? Try just doing json_encode($temp);
精彩评论