Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#10404 closed bug (invalid)

Cannot use global variables?

Reported by: r.haeder@… Owned by:
Priority: low Milestone: None
Component: ajax Version: 1.6.4
Keywords: Cc:
Blocked by: Blocking:

Description

Hi,

I have the following (example) code to explain my trouble

var isFooRequested = false;

function requestFoo () {
  // By default nothing is requested
  this.isFooRequested = false;

  $.ajax({
    url: 'ajax.php',
    type: 'POST',
    data: 'foo=foo',
    dataType: 'json',
    async: false,
    success: function (ajax_content) {
      // Do something here with the content
      
      // Set it, because foo has been requested
      this.isFooRequested = true;
    },
    error: function (ajax_content) {
      // Do something with the error here, but do not change isFooRequested
    }
  });

  // This does never return true, even on 'success' of foo-request
  return this.isFooRequested;
}

I also tried to rewrite it to setter/getter and used explicit but it doesn't work either. So what do I do wrong?

Regards, Roland

Change History (5)

comment:1 Changed 8 years ago by Rick Waldron

Component: unfiledajax
Priority: undecidedlow
Resolution: invalid
Status: newclosed

This is not a bug in jQuery, please post support questions on the forum.

comment:2 Changed 8 years ago by r.haeder@…

Na, not again registration. :( :( :(

comment:3 Changed 8 years ago by r.haeder@…

It might not be a direct bug in jQuery, okay. But it looks like a bug to a user (which I am of jQuery). So please just let me know what is wrong here. Then I don't need to register for a single question. :(

comment:4 Changed 8 years ago by Rick Waldron

$.ajax calls happen asynchronously, so your function returns before the ajax call does. Additionally, this isn't a "global" variable and |this| isn't what you've created an expectation of inside of the $.ajax "success" callback

comment:5 Changed 8 years ago by ajpiano

For future reference this bug tracker is for bugs that are directly in jQuery, not bugs that result from errant usage thereof or from misunderstandings of the way that JavaScript works.

Note: See TracTickets for help on using tickets.