Bug Tracker

Modify

Ticket #10404 (closed bug: invalid)

Opened 20 months ago

Last modified 20 months ago

Cannot use global variables?

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

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

comment:1 Changed 20 months ago by rwaldron

  • Priority changed from undecided to low
  • Resolution set to invalid
  • Status changed from new to closed
  • Component changed from unfiled to ajax

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

comment:2 Changed 20 months ago by r.haeder@…

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

comment:3 Changed 20 months 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 20 months ago by rwaldron

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.