views:

39

answers:

4

I would like to do something like this:

  function blabla();

  var check = blabla(); 
  if(check == 1) 

But in my blabla function I have a Get request and I have a callback but can't get the return value to be the blablas return value;

var checkemail = 0; 
    $.get("bla.aspx", {
        day: "friday"
    },
        function(data) {
            console.log(data); 
            checkemail = data;
        });

console.log("checkemail: " + checkemail);
if (checkemail == '1') {

The problem is that the data variable don't assign the checkemail variable. Why does it not?

A: 

Can you move your if (check == 1) { ... } code into the callback?

Bobby Jack
But then I must have like 100 lines of code in the callback. It's pretty ugly i think.
Jaffa
Than you can move the code to a separate function like john_doe and I suggested.
jigfox
+1  A: 

What about...?

function checkMail(data) {
   if (data == '1') {
      ...
   }
}

$.get("bla.aspx",
      { day: "friday" },
      function(data) {
          console.log(data); 
          checkMail(data);
      }
);
john_doe
A: 

This is an AJAX request and the A stands for Asynchronous, that means your code doesn't stop and wait for the response, unless you say so. $.get is only a wrapper around $.ajax. If you want the request to be synchronous, you need to use the $.ajax instead like this:

var checkemail = 0; 
$.ajax({
  async: true,
  url: "bla.aspx",
  data: {day:"friday"},
  dataType: text,
  success: function(data){
    console.log(data);
    checkmail = data;
  }
});
console.log("checkemail: " + checkemail);
if (checkemail == '1') { /* do something */ }

Or you can use the callback function to check the data. A callback function is the function you defined in the success attribute. This function gets called after the request is finished. So you are able to do something like this:

$.get("bla.aspx", {
    day: "friday"
  },
  function(checkmail) {
    console.log("checkemail: " + checkemail);
    if (checkemail == '1') { /* do something */ }
  }
);

if the /* do something */ is very long you can put it into a separate function and than put the function call to the get request like this:

var do_something = function(checkmail) {
  console.log("checkemail: " + checkemail);
  if (checkemail == '1') { /* do something */ }
}

$.get("bla.aspx", {
    day: "friday"
  },
  do_something // note the missing `()`, it must be like this
);
jigfox
A: 

I did some code like this and it worked fine.

$.get("/url.php", function(reply){
alert(reply);
});

Perhaps you need to check whether :

  1. bla.aspx does return something. It's kind of ridiculous debugging your code while the error lies in another place.
  2. And if the bla.aspx does return something. Just make sure that it's not null or an empty string.
  3. Try with another page. Maybe, just maybe, bla.aspx page consumes a lot of time before returns something. This happened to me.

In my opinion, nothing's wrong with your code :) Oh i missed something. It's good to replace your function body with alert() statement just to make sure that you don't make any typing error. If you already tried this, please ignore.

jancrot