Ajax and JSON request problems

Bug #934297 reported by Don Schoeman
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
PHPDevShell
Fix Released
High
TitanKing

Bug Description

PHPDevShell version 3.0.1 and 3.0.3.

The Ajax menu type (10) currently always assume that HTML will be returned from the server when it may also be XML or JSON. It also by default injects some LightBox script into its results which obviously breaks any XML or JSON results. The Lightbox html looks like this:
<script type="text/javascript">PHPDS_documentReady('#PHPDS_lightbox');</script> <!-- LIGHTBOX -->
<div id="PHPDS_lightbox">[JSON RESULT or XML RESULT]</div>

Currently the only way to get around this problem is to configure the menu as a Standard menu (1 - Standard Web Page) and to set its Theme to empty. Then lastly one have to also add the header "application/json" using: header("Content-Type: application/json"); in your controller.

I have two suggestions:
Suggestion A) Add two more menu types namely Ajax XML and JSON and let it behave in the correct way.

Suggestion B) Change the Ajax menu to read an additional parameter or request header to determine the output result. For example, one should be able to either add a URL parameter such as datatype=ajax / datatype=json or perhaps even better the "Accept:" request header should be adhered to. For example a standard JQuery JSON request header looks like this: "Accept: application/json text/javascript" and PHPDevshell can pick up on this to know how to respond to the request.

I personally do not like the fact that one has to pass a URL parameter through to let PHPDevShell know what it is dealing with. If one forgets to add ajax=true to the Ajax request URL the server returns with an invalid or empty response which buggers the handling on the Javascript side. This is actually quite a hard problem to try and resolve.

We also need to look at the error responses when dealing with Ajax/JSON. If it is up to the end-developer to implement better Ajax error handling mechanisms then it should at least be documented well.

Revision history for this message
Don Schoeman (don.sch) wrote :

I did a bit of research on the topic of the "Accept: application/json" header that is sent with the Ajax request. This header along with "X-Requested-With: XMLHttpRequest" is only sent by some javascript libraries such as JQuery and MooTools so one can not always rely that it will be there. I think the best would be to first test for the header and if that doesn't exist one should check for a URL parameter such as datatype=json.

Revision history for this message
TitanKing (titan-phpdevshell) wrote :

I do note the complication, for raw ajax data the idea was to use the viaAjax method, the three menu types mentioned above is just for html related content, this might be a confusing idea. However, to resolve your current issue, you must change your menu type to normal, and use the viaAjax method to receive the data.

Lets discuss this further when you are online so we can also find a resolution together.

Revision history for this message
Greg (gregfr) wrote :

I agree with Jason, the viaAjax() method is the way to go.

See:
http://www.phpdevshell.org/content/ajax-controller-and-asynch-templates
http://www.phpdevshell.org/content/return-values-controllers

The framework should not add its own GET/POST parameters, they are only for the developer. Headers are there for the framework's internal stuffs.
Ajax always need some code on the browser side, so that's where the header should be added. Maybe we can provide some "independant" code for calling ajax, but writing cross browser JS without a library is really a risky approach.

Revision history for this message
Don Schoeman (don.sch) wrote :

Hi Greg, I think I may not have made myself clear in the post. Having custom GET/POST vars is definitely not what I requested. I simply meant that some of the javascript libraries do pass along the HTTP_X_REQUESTED_TYPE header and that we could use it. I wasn't aware that you are already using it in 3.0.5 so thanks for clearing that up.

Changed in phpdevshell:
status: New → Confirmed
assignee: nobody → TitanKing (titan-phpdevshell)
Revision history for this message
TitanKing (titan-phpdevshell) wrote :

I am assigning this as a valid bug, the reason being the following:

1. Write proper documentation.
2. Improve node descriptions (Ajax Widget, Ajax LightBox, Ajax HTML, Ajax Raw (XML/JSON, etc)
3. Add raw ajax node type just for correctness, while leaving viaAjax as is.

I will tackle this very soon.

Changed in phpdevshell:
status: Confirmed → Fix Committed
Revision history for this message
TitanKing (titan-phpdevshell) wrote :

Hi Guys,

a Nice push has been made. I have added a live search example in the Example Plugin. The live search is using node Raw Ajax Type specifically and returning json in this case. Also the Ajax handler was improved allot, it cuts out the whole template system when raw ajax is requested with Raw Ajax Node Type. This makes ajax calls almost instant.

Revision history for this message
Greg (gregfr) wrote :

Just two thoughts about this:

- you don't have to set the template to empty, just return anything but false from the controller and the template will not be displayed (http://www.phpdevshell.org/content/return-values-controllers)
- you can call a PHP method from JS with PHPDS_remoteCall() which takes care of headers for you

Changed in phpdevshell:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.