0

I am trying to use the following code:

$('body').bind('ajaxSend', function (elm, xhr, s) {
    if (s.hasContent && securityToken) {   // handle all verbs with content
        var tokenParam = "__RequestVerificationToken=" + encodeURIComponent(securityToken);
        s.data = s.data ? [s.data, tokenParam].join("&") : tokenParam;
        // ensure Content-Type header is present!
        if (s.contentType !== false || options.contentType) {
            xhr.setRequestHeader( "Content-Type", s.contentType);
        }
    }
});

I found this in the post Stack Overflow post

With typescript I get a syntax error on "options" saying "the name options does not appear in the current scope.

Can someone help and explain why I am getting this error. I see options is not declared and wonder where it comes from if it's not declared.

Community
  • 1
  • 1
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

2 Answers2

0

It's just like with JavaScript where, at run-time, any enclosing function scopes and then the global scope are searched for an options variable and if it isn't found anywhere an exception is thrown because it's not defined.

So presumably, in the example code you pulled this from, you should assume that options is defined elsewhere, and you'd need to do the same.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
0

I believe the slight mistake is that this line:

 if (s.contentType !== false || options.contentType) {

Should actually be:

 if (s.contentType !== false || s.contentType) {

As your parameter s is the ajaxOptions passed to your function by jQuery.

Fenton
  • 241,084
  • 71
  • 387
  • 401