0

I want to overwrite the status variable from the ajax call.. i tried in this way but its not overwriting the status variable... Please help me.

 function myfunction()
            {
                var status = "";
                $.ajax({
                    type: 'POST',
                    url: "<?php echo base_url() ?>login/ajaxsessioncheck/",
                    success: function(data)
                    { 
                            status="new value"; 

                    }
                });
                alert(status)
            return status;
            }
Dan
  • 2,086
  • 11
  • 71
  • 137
  • 2
    you can't do that - http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Arun P Johny Mar 02 '14 at 15:27
  • 2
    You can't return from an async function like that. – adeneo Mar 02 '14 at 15:28
  • @adeneo so how to solve this issue in some other way? i really need to solve this – Dan Mar 02 '14 at 15:29
  • @ArunPJohny Question which you are showing is diffrent then my one. Did you understand my question? – Dan Mar 02 '14 at 15:30
  • 1
    it is essentially the same, you are trying to return a value which is set by the ajax response... – Arun P Johny Mar 02 '14 at 15:39
  • 2
    @Sundara, the linked question is EXACTLY what you are trying to do here. As other answers said, the call is asynch, the code keeps executing. You can put `async: false` in the ajax options but that will defeat the purpose of async remote calling... – Tallmaris Mar 02 '14 at 15:40
  • @Tallmaris Okay i am reading.. lets you know if i have any confusion regarding it.. – Dan Mar 02 '14 at 15:43
  • @ArunPJohny You are right... I could not see it thats why i posted this duplicate question. – Dan Mar 02 '14 at 15:44
  • @AshutoshSingh hey... You got solution of this question? – tejash patel Nov 30 '18 at 05:36

3 Answers3

2

A possible solution could be something like,

function ajaxsessioncheck(myCallback)
            {
                var status = "";
                $.ajax({
                    type: 'POST',
                    url: "<?php echo base_url() ?>login/ajaxsessioncheck/",
                    success: function(data)
                    { 
                            //status="new value"; 
                        myCallback(data);

                    }
                });
//                alert(status)
//            return status;
            }

So what you probably want to do is,

if(ajaxsessioncheck()){
//then session still exists
}

but you should actually do something like,

ajaxsessioncheck(function(data){

  status = "new value";
  alert(status);
//go to next page 
//or do next action that is only allowed if session still exists

});

Also the thread mentioned in the comment by Arun P Johny ,How do I return the response from an asynchronous call? ,provides a thorough explanation of how to tackle this specific problem and the issues related to the synch and asynch behaviour of ajax requests.

Community
  • 1
  • 1
melc
  • 11,523
  • 3
  • 36
  • 41
1

You're lacking basic understanding of how AJAX calls work. AJAX calls are asynchronous. Your code keeps executing while the call is being processed. The code inside your success callback will only execute after the current executing code finishes (when javascript relinquishes control back to the browser) and when the AJAX request is finished. If you put the alert after status = "new value" you'll see it gets the right value.

Oscar Paz
  • 18,084
  • 3
  • 27
  • 42
0

FIDDLE

An ajax call is Async by default so when you return status the new value for status is probably not set. Either make your ajax call synchronous or define your status variable in the outer scope and then just set it inside the success callback.

var status = "";
alert(status);    
function ajaxsessioncheck(){ 
    $.ajax({
         type: 'POST',
         url: "<?php echo base_url() ?>login/ajaxsessioncheck/",
         success: function(data){ 
              status="new value"; 
              alert(status);
              // write code for additional functionality that needs to be executed after the new value has been set for status here.. of just make a call to another function that handles further operations..
              doSomethingWithNewStatus();
         }
   });
}

ajaxsessioncheck();


function doSomethingWithNewStatus() {
 alert("Here is your new status " + status);
}
Lucky Soni
  • 6,811
  • 3
  • 38
  • 57
  • So basically two times checking .. ajaxsessioncheck(), i think this will make my application slow.. what you say? – Dan Mar 02 '14 at 15:40
  • I seriously doubt the above will work... – Tallmaris Mar 02 '14 at 15:41
  • @Tallmaris check the fiddle to be sure :) – Lucky Soni Mar 02 '14 at 15:44
  • @Sundara checking two times? could you please explain more? – Lucky Soni Mar 02 '14 at 15:46
  • Why downvote?? Please leave a comment. – Lucky Soni Mar 02 '14 at 15:46
  • @LuckySoni First time it is calling `ajaxsessioncheck();` right ? then again calling `ajaxsessioncheck();` that 2 times.... If i am doing huge task under this `ajaxsessioncheck();` funtion then my project will be very slow...Right? _ & i have not did any downvote, i always support people. – Dan Mar 02 '14 at 15:48
  • @Sundara Please check my code carefully.. there is just one call to `ajaxsessioncheck();` i have defined the function once and then called it just once! – Lucky Soni Mar 02 '14 at 15:51
  • Will someone tell me why a downvote?? Its not the downvote but i really don't understand the problem with my answer.. Help me learn!! :) I am happy to receive another 100 downvotes but just give me the reason! – Lucky Soni Mar 02 '14 at 15:56
  • Hi Lucky, I downvoted before you edited your answer and added working code... I removed the downvote now. :) – Tallmaris Mar 02 '14 at 16:04
  • poor u. here another +1 to balance it:P – Mohammed Joraid Mar 02 '14 at 16:04
  • but keep in mind that doesn't answer the OP question. He is expecting a return from ajaxsessioncheck(). – Mohammed Joraid Mar 02 '14 at 16:06
  • thank you :) I am glad that i am doing the things the right way.. @Tallmaris yes, i placed that `alert` in the wrong place. – Lucky Soni Mar 02 '14 at 16:06
  • @Joraid Does `returning a value` not make it available to the seeker? Is my answer not doing that? – Lucky Soni Mar 02 '14 at 16:08
  • @Joraid You are right. Does not solve my problem with this answer. – Dan Mar 02 '14 at 16:10
  • @LuckySoni You are right, your answer is not calling 2 times the same function. But you did not answer in the way how i was looking for it. But anyway thanks for your time – Dan Mar 02 '14 at 16:11