We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Ajax request problem

I have problem with the function isAjax(), it always return false even when i add the header "HTTP_X_REQUESTED_WITH" to "XMLHttpRequest", and when i print out the header info it is empty, too. I'm now using ubuntu 14.04 64bits, my code used to work well on ubuntu 12.04. Please help me.



43.9k

Hi,

check if there is no javascript/jQuery errors



2.1k

I have checked the javascript code, i have also tried to send the request from a rest app with the setting of "HTTP_X_REQUESTED_WITH" in the header, but when printing out it is still empty.



2.1k

After trying some value, i figure out that my function $request->getHeader('key') does not accept the 'key' which contains the special character. It can print out value if it was set with "HTTPXREQUESTEDWITH" (some other values which don't have any special character). Did i miss-configure something with the server ?



12.2k
edited Sep '14

This problem do not related to the server or Phalcon at all.

I had exact same problem with AngularJS. You have to manually add the Header XMLHttpRequest After that in the $_SERVER variable will appear the key 'HTTP_X_REQUESTED_WITH' I assume that you are using JQuery

$.ajax({
    url: 'foo/bar',
    headers: { 'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' }
});

How can I add a custom HTTP header to ajax request with js or jQuery?

Also FYI some times there can be problems with Cross-Domain requests. Please see https://api.jquery.com/jquery.ajax/ crossDomain section of the manual. I do not say that it's related to your problem, but I just let you know about possible issues