Welcome Guest, Not a member yet? Register   Sign In
is_ajax_request does not recognized XMLHttpRequest

(This post was last modified: 10-04-2018, 06:41 AM by ciadmin. Edit Reason: Edited formatting )

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()) {
// Continue to process

var ajax = new XMLHttpRequest();
           ajax.open("POST", ajaxUrl, true);
           ajax.onreadystatechange = function () {
               if (this.readyState == 4) {
                   if (this.status == 200) {
                       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 != "") {
               } else if (this.readyState == 2) {
                   if (this.status == 200) {
                       this.responseType = "blob";
                   } else {
                       this.responseType = "text";

(This post was last modified: 10-04-2018, 10:04 AM by InsiteFX. Edit Reason: add lines )

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.

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 )

You need to manually specify the requested-with method with plain JS. jQuery and other libraries does it for it like InsiteFX said.
//Send the proper header information along with the request

(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.

Theme © iAndrew 2016 - Forum software by © MyBB