PDA

View Full Version : JSON response not working



Tim
20 Jun 2007, 10:36 PM
I'm trying to get my head around the JSON data requests (first time I've attempted this). Because I couldn't find any PHP based examples of returning JSON data to Ext, I've tried to reverse engineer the remote-topics.php example in the grid/paging demo.

This is the link to the JSON data I'm using (just copied the request output from the demo):

http://lab.realityedge.com.au/rem/json-test/service.php

The code for this is here:



<?php

header('Pragma: no-cache');
header('Cache-Control: no-cache, must-revalidate');
header('Last-Modified: Thu, 21 Jun 2007 05:40:05 GMT');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

require 'JSON.php';

// create a new instance of Services_JSON
$json = new Services_JSON();

$value = array(
"totalCount" => "2166",
"topics" => array(
array("topic_id" => "4553",
"forum_id" => "4",
"topic_title" => "How to Post Code Samples with color ?",
"topic_poster" => "5184",
"topic_time" => "1175350248",
"topic_views" => "34",
"topic_replies" => "1",
"topic_status" => "0",
"topic_vote" => "0",
"topic_type" => "0",
"topic_first_post_id" => "20410",
"topic_last_post_id" => "20455",
"topic_moved_id" => "0",
"username" => "Mohammed",
"user_id" => "5184",
"user2" => "Mohammed",
"id2" => "5184",
"post_username" => "",
"post_username2" => "",
"post_time" => "1175388943",
"post_text" => "post text goes here")
)
);

$output = '(' . $json->encode($value) . ')';

header('Content-Length: ' . strlen($output));

print($output);

?>


Other than the post_text value, the output is basically the same as the example:

http://www.yui-ext.com/forum2/topics-remote.php?start=0&limit=1&forumId=4


So why cant I replace the url in the demo with my one? Mine just times out and displays nothing in the grid output. But if you load it through the browser, even comparing the request headers, it looks ok to me.

Any ideas what I've done wrong or missed?

Animal
20 Jun 2007, 11:55 PM
Are you using the ScriptTagProxy?

Read the documentation for it to check your PHP script complies.

Tim
21 Jun 2007, 10:53 PM
Yes, I'm using ScriptTagProxy. All I did was replace the url in the ScriptTagProxy in the grid example.

Weirdly enough it turns out I can get it working by changing it to HttpProxy when they are clearly on different domains. Json output is being served by lab.realityedge.com.au and the html file is being run locally on my machine while I'm developing using the in built browser in EditPlus2.

Any ideas why it works opposite to the documentation? From what I understand, it should require the use of ScriptTagProxy to make it work.

Tim
21 Jun 2007, 10:56 PM
BTW, checking the online docs, there is no mention of what the PHP script is supposed to output to comply with a JSON script.

I'm looking here: http://extjs.com/deploy/ext/docs/index.html and in the Ext/Data/ScriptTagProxy listing.

Am I looking in the wrong place?

comm
22 Jun 2007, 1:02 AM
Original code:


...
$output = '(' . $json->encode($value) . ')';
...

SHOULD BE:


...
$output = $json->encode($value);
...

You don't need the "braces".

Tim
24 Jun 2007, 5:02 PM
I tried it originally without the brackets and it doesn't work. Comes up with a parse error when Ext tries to eval the output. Check Google, seems to be a common problem. When I check the output of the example JSON, it has brackets around the output as well.

The annoying part is my JSON is character for character exactly the same as the example feed from this website, and mine doesn't work with the ScriptTagProxy, but does with the HttpProxy