CakePHP Concurrent AJAX Requests Blocking - ajax

I am working with CakePHP on an app which has to run a time-consuming task via a single AJAX call, with secondary periodical AJAX calls checking on the progress of the task.
The Problem
While the time-consuming task (which takes >30 seconds) is running via it's AJAX request to CakePHP, the secondary progress AJAX request seems unable to be "blocking".
To clarify, the secondary progress AJAX request does not return any error, it simply does not return any response until the original time-consuming request finishes.
Once this original AJAX request finishes, the secondary progress AJAX request returns as expected.
It seems that execution of the progress request is being queued until the first AJAX call finishes, as the progress returned is 100%.
What I've Tried
I have tried multiple suggested solutions, including:
Changing the session handler to 'cake' in core.php - no fix
Setting the config security level to 'medium' in core.php - no fix
Disabling user agent checks in core.php - no fix
Testing multiple concurrent AJAX calls to a plain PHP script on the same server - works as expected
Any Ideas?
So it seems as though the issue is caused by CakePHP - has anyone experienced this in their own CakePHP app?
Thanks!

Session handling is set to file in php
from php.ini
[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = files
This prevent php from running more than one instance of session per user.
To prevent this run this in your php code:
session_write_close();
Just know session is now closed, so writing to session is no longer an option.

For some clarification, are you using the built-in AJAX helper (on prototype) or some external library like jQuery?
Usually, the javascript library of choice has an {async: true} available to force concurrency. See this example:
$.ajax({
type: 'GET',
url: '/fetch.php',
async: true,
success: function(data, status) {
$('#status').html(status);
}
});
For the built-in CakePHP AJAX helper, this option should do the trick: $options['type']. More info here. Do note that the AJAX helper is deprecated as of version 1.3 and the jsHelper should be able to takeover, see the request() method here for instance (also has an option called async).

Related

Symfony2, jQuery Ajax request, web debug toolbar, 500 Internal Server Error

I am developing an application with Symfony2 platform that, at one moment, makes a simple AJAX request on document ready.
jQuery.ajax({
url: 'test.php',
type: 'POST',
data: {
'test': 'test'
},
success: function( response ){
jQuery( 'div#container' ).html( response );
}
});
The problem is that on app_dev.php, where the debug toolbar is loaded, an 500 error is throwed.
More precisely, the AJAX response from the 'test.php' file is received and loaded in the div container, but then, when it tries to load the toolbar, I receive an alert message:
"An error occured while loading the web debug toolbar (500: Internal
Server Error). Do you want to open the profiler?"
If I open profiler, I don't see anything wrong.
If I open the xhr request for the toolbar, a FastCGI 500 error is displayed:
Module FastCgiModule
Notification ExecuteRequestHandler
Handler PHP54_via_FastCGI
Error Code 0x000000ff
If I don't try to make the AJAX request, the toolbar is loaded with no problem.
If I make the request by hand (event on a button) after the toolbar is loaded, again, no problem. If I make the request by hand (event on a button) before the toolbar is loaded the error is throwed.
Notes:
This error occur only on the local machine (with ISS 7.5, PHP 5.4.14). On the production server (IIS server 8.0, same and PHP) the AJAX request is made, and the toolbar is loaded with no problems. I have replaced the php.ini on the local machine with the php.ini from the production server - same problem.
Initialy AJAX request was loading the result of a simple bundle controller method, but then I have tried with a simple PHP file 'test.php', same problem.
I have tried with post and get methods to make the request.
Does anyone have any ideea what goes wrong?
This is not a serious problem since I have multiple options the develop this app, but is bugging me, and I already lost a enough time with this.
PS: if it make any difference, on same machine I have developed an application - no framework - that makes, multiple simultaneos ajax requests.
The following will not fix your error, but provides an alternative way to possibly achieve your need. It seems you want to load asynchronously a part of your website, Symfony2 provides a solution for that: ESI render.
The doc is here: http://symfony.com/doc/current/book/templating.html#asynchronous-content-with-hinclude-js
This method will create an ajax call only then your page will be rendered.

Running Tinymce multiple ajax calls issue

I have been sending multiple ajax calls from a single page and trying to load TinyMCE with each ajax call. But TinyMCE loads only the first time.
The code I have been using after AJAX success:
success: function(html) {
$('#showmail').html(html);
$(".mceSimple").each(function(){
tinymce.execCommand('mceRemoveControl',true,'elm1');
tinyMCE.execCommand("mceAddControl",true, 'elm1');
});
Could someone tell me what I am doing wrong
You need to shut down tinymce correctly before reinitializing an editor with the same id a second time.
To shut down an edtor instance use:
tinymce.execCommand('mceRemoveControl',true,'editor_id');
To reinitialize use
tinymce.execCommand('mceAddControl',true,'editor_id');

prevent ajax request caching in IE during post method

How to prevent IE from caching the request sent to the server?
i tried by setting ("Cache-Control: no-cache) in the https response object but still the IE is caching my request data.
Please find tmy project details as below:
in my application i am sending login request to the server. so after i login if i take the memory dump using winHex tool i am able to get the password details in the memory.
i am clearing the dialog refrense also but still the request data is getting cached.
Please suggest me some work arround for this
You could try to add a parameter to your URL with a random value, this will prevent that the URL is always thesame.
Example:
Normal URL:
www.test.com/test.php
Fake different URL:
www.test.com/test.php?_dc=12353somerandomval
Make sure the _dc parameter always has a different value, you can (for example) use JavaScript date object for this (It returns the current time in milliseconds, which will virtually always be different):
params: {
_dc : new Date().getTime()
}
In a project I did a while back I had the exact same issues, I searched around and saw a few things that recommended adding a time stamp to the request, that does work too, but this was the most elegant way that worked for me.
$('document').ready(function () {
$.ajaxSetup({
cache: false
});
});

Cross domain javascript ajax request - status 200 OK but no response

Here is my situation:
Im creating a widget that site admins can embed in their site and the data are stored in my server. So the script basically has to make an ajax request to a php file in my server to update the database. Right? Right :)
The ajax request works excellent when i run it in my local server but it does not work when the php file is on my ONLINE server.
This is the code im using:
var url = "http://www.mydomain.net/ajax_php.php";
var params = "com=ins&id=1&mail=mymail#site.net";
http.async = true;
http.open("POST", url, true);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
//do my things here
alert( http.responseText );
}
}
http.send(params);
In firebug it shows: http://www.mydomain.net/ajax_php.php 200 OK X 600ms.
When i check the ajax responnseText I always get a Status:0
Now my question is: "Can i do cross-domain ajax requests by default? Might this be a cross-domain ajax problem? Since it works when the requested file resides in my local server but DOESN'T work when the requested file is in another server, im thinking ajax requests to another remote server might be denied? Can you help me clear on this?
Thanks..
Cross-domain requests are not directly allowed. However, there is a commonly-used technique called JSONP that will allow you to avoid this restriction through the use of script tags. Basically, you create a callback function with a known name:
function receiveData(data) {
// ...
}
And then your server wraps JSON data in a function call, like this:
receiveData({"the": "data"});
And you "call" the cross-domain server by adding a script tag to your page. jQuery elegantly wraps all of this up in its ajax function.
Another technique that I've had to use at times is cross-document communication through iframes. You can have one window talk to another, even cross-domain, in a restricted manner through postMessage. Note that only recent browsers have this functionality, so that option is not viable in all cases without resorting to hackery.
You're going to need to have your response sent back to your client via a JSONP call.
What you'll need to do is to have your request for data wrapped in a script tag. Your server will respond with your data wrapped in a function call. By downloading the script as an external resource, your browser will execute the script (just like adding a reference to an external JS file like jQuery) and pass the data to a known JS method. Your JS method will then take the data and do whatever you need to do with it.
Lots of steps involved. Using a library like jQuery provides a lot of support for this.
Hope this helps.

Is there a limit to the number of Ajax requests that can be launched on Apache

Is there a limit to the number of simultaneous Ajax requests than can be launched toward an Apache server? For example, consider the following function to update div elements on a page (prototype JS):
function trigger_content_update(cell) {
//asynchronous : false is required for this to work properly
$$('.update').each(function(update_item){
new Ajax.Request('/neighbouring?.state=update_template&dummy='+(new Date()).getTime(),{
asynchronous: false,
parameters: {divid: update_item.id, source: cell},
onComplete: function(response) {
var elm = response.getHeader('Element');
if ($(elm) !== null) { $(elm).update(response.responseText) }
}
});
});
}
On my HTML page, there are 8 div elements that are marked with the "update" CSS selector, thus launching 8 ajax requests. The code works fine with the asynchronous property set to false, but as soon as i set asynchronous:true i can observe (in Firebug) most Ajax requests returning a 500 status (internal server error).
Once this occurs, it is required to restart apache to recover.
I'd check the server side code that's handling the requests.
As far as Apache is concerned, your Ajax request is just a POST - the same as if you'd submitted a form. 8 simultaneous requests should easily be handled by Apache, so it suggests that the server side code that Apache is running is locking up - perhaps it's trying to write to a data file and finding it locked?
I just wrote a test case where I sent out 10,000 simultanuous Ajax calls to a service. Works fine on Apache Tomcat. All service came back with a proper answer.
It sounds like your service is having some internal synchronization issues.

Resources