• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is_ajax_request does not recognized XMLHttpRequest

#1
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);
Reply

#2
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
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');
Reply

#4
(10-04-2018, 11: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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.