Skip to main content

Bug Tracker

Side navigation

#10404 closed bug (invalid)

Opened October 02, 2011 08:43PM UTC

Closed October 02, 2011 08:47PM UTC

Last modified October 02, 2011 09:28PM UTC

Cannot use global variables?

Reported by: r.haeder@gmx.de 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

Attachments (0)
Change History (5)

Changed October 02, 2011 08:47PM UTC by rwaldron comment:1

component: unfiledajax
priority: undecidedlow
resolution: → invalid
status: newclosed

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

Changed October 02, 2011 08:51PM UTC by r.haeder@gmx.de comment:2

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

Changed October 02, 2011 09:05PM UTC by r.haeder@gmx.de comment:3

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

Changed October 02, 2011 09:19PM UTC by rwaldron comment:4

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

Changed October 02, 2011 09:28PM UTC by ajpiano comment:5

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.