is_ajax_request does not recognized XMLHttpRequest - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6) +--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19) +--- Thread: is_ajax_request does not recognized XMLHttpRequest (/showthread.php?tid=71874) |
is_ajax_request does not recognized XMLHttpRequest - tecthon - 10-04-2018 Dear All, I got issue, below php code is in controller, when i do jquery ajax requests it works but when i make a call with JS Code mentioned below it wont work. if remove (!$this->input->is_ajax_request()) check. below js code work. please anyone can suggest me what i am doing wrong or what i should do to keep (!$this->input->is_ajax_request())check, this check is important. PHP Code: if (!$this->input->is_ajax_request()) { Javascript Code: var ajax = new XMLHttpRequest(); RE: is_ajax_request does not recognized XMLHttpRequest - InsiteFX - 10-04-2018 Not sure what JavaScript or framework your using some do not send the HTTP_X_REQUESTED_WITH header. jQuery doe's send it. You can try this and see what you get this is the old way that we use to do it. Code: define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); You can place the define in your ./application/config/constants.php file or were you want. You may need to send the header yourself. RE: is_ajax_request does not recognized XMLHttpRequest - jreklund - 10-04-2018 You need to manually specify the requested-with method with plain JS. jQuery and other libraries does it for it like InsiteFX said. Code: //Send the proper header information along with the request RE: is_ajax_request does not recognized XMLHttpRequest - dave friend - 10-04-2018 (10-04-2018, 10:02 AM)InsiteFX Wrote: You can try this and see what you get this is the old way that we use to do it. The above (more or less) what CI_Input does From /system/core/Input.php PHP Code: public function is_ajax_request() I think there is something in the JavaScript that is producing a non-standard XMLHttpRequest. |