CodeIgniter Forums
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()) {
 
redirect('backend');
 
 exit();
}else{
 
// Continue to process


Javascript
Code:
var ajax = new XMLHttpRequest();
           ajax.open("POST", ajaxUrl, true);
           ajax.onreadystatechange = function () {
               if (this.readyState == 4) {
                   if (this.status == 200) {
                       console.log(this.response);
                       console.log(typeof this.response); // should be a blob
                       var blob = new Blob([this.response], {type: "application/octet-stream"});

                       //saveAs(blob, file_name);
                   } else if (this.responseText != "") {
                       console.log(this.responseText);
                   }
               } else if (this.readyState == 2) {
                   if (this.status == 200) {
                       this.responseType = "blob";
                   } else {
                       this.responseType = "text";
                   }
               }
           };
           ajax.send(null);



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');

if(IS_AJAX) {
   ... it's an AJAX request so do something ...
}
else {
   ... it's not an AJAX request so do something else ...
}

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
ajax.setRequestHeader("X-Requested-With",'xmlhttprequest');



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.

[code]define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

The above (more or less) what CI_Input does

From /system/core/Input.php
PHP Code:
   public function is_ajax_request()
 
   {
 
      return ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
 
   

I think there is something in the JavaScript that is producing a non-standard XMLHttpRequest.