Search code examples
javascriptjqueryphpstormjsdom

jQuery beforeSend is now an unused property?


I was using this exact function before without any errors. Now, PhPStorm (10.0.1) suddenly warns me that the beforeSend property is not being used. I thought it must be a false positive due to some update to PhPStorm and how it lints.

This is not the case, it really is not being called any more! But I can't for the life of me understand why:

jsdom.env({
    html: '<html><body></body></html>',
    scripts: ['https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.js'],
    done: function adminAuth(err, window) {
      var $ = window.jQuery;
      $.ajax({
        crossDomain: true,
        url: client.globals.api + 'auth/admins',
        method: 'POST',
        processData: false,
        data: '{"email": "' + client.globals.credentials.username + '", "password": "' + client.globals.credentials.password + '"}',
        beforeSend: function(request) {
          request.setRequestHeader('accept', 'application/json');
          request.setRequestHeader('content-type', 'application/json');
          request.setRequestHeader('authorization', 'Basic NotForYourEyes');
          request.setRequestHeader('cache-control', 'no-cache');
        },
        success: function(response) {
          captureStoreId(response, $);
        },
        error: function(jqXHR, textStatus, err) {
          console.log('Authentication failed with status %s and error %s', textStatus, err);
        }
      });
    }
  });

The error logged in console is:

Authentication failed with status error and error null

Which is (some sort of) expected, since the authentication header is not being added.

Again, this exact snippet of code, including the way it is called, has not changed one bit. Same with the credentials of the server. No other error is present in the console.

Any help is much appreciated!


Solution

  • After logging something inside of beforeSend it became apparent that it was indeed still being executed.

    The error was with my credentials, which I updated around the same time I noticed the false linting error from Phpstorm. So, I was a victim of assuming the output in Phpstorm was correct. Never assume... sigh.

    TLDR: this is a false positive linting error from Phpstorm. beforeSend still works using this version 3.1.2 of jsdom and Google's hosted version 2.1.4 of jquery.