Javascript variable scope

Hello, I have this function.
function save_to_db(item)
    {
        var widget_id;
        widget_name = item.attr('id'); //ie - image_widget
        alias = widget_name.replace("_widget", "");

        $.ajax({
         //ajaxsetup ftw
          data: {
              'action': 'add_element',
              'page_id': '106034386126175',
              'element_id' : alias
          },
          success: function(msg)
                   {
                       widget_id = msg;
                   }
        });

        return widget_id;
}

Open in new window


I want this function to return the data which comes from the ajax success, however it's always returning "undefined" and the ajax success works correctly. Any idea?
LVL 1
sahanzAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
we need to add : .responseText;

var widget_id = $.ajax({ url: "some.php", async: false }).responseText;

so :


function save_to_db(item) {
   widget_name = item.attr('id'); //ie - image_widget
   alias = widget_name.replace("_widget", "")
   var d = data: { 'action': 'add_element', 'page_id': '106034386126175', 'element_id' : alias }
   return $.ajax({ url: "some.php", async: false }).responseText;
}

Open in new window

0
 
HainKurtSr. System AnalystCommented:
try

success: function(msg)
                   {
                       widget_id = msg; alert(widget_id);
                   }

what does it show?
0
 
Gurvinder Pal SinghCommented:
it cannot come from success handler, since success handler is a call back handler which is called asynchronously.

what you need to do is, that the method which needs the widget id needs to get called from the success handler.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
HainKurtSr. System AnalystCommented:
I guess you should use async=false

async: false
$.ajax({
         //ajaxsetup ftw
          async: false,
          data: {
              'action': 'add_element',
              'page_id': '106034386126175',
              'element_id' : alias
          },
          success: function(msg)
                   {
                       widget_id = msg;
                   }
        });

Open in new window

0
 
sahanzAuthor Commented:
@HainKurt:

Works fine, widget_id alerts the correct value.
0
 
HainKurtSr. System AnalystCommented:
I guess you should change your logic a bit :) if you do not want to use async
when it succeded, call another function to do the remaining jobs (if any)

success: function(msg)
                   {
                       widget_id = msg; fnDataSaved(msg);
                   }

but did you try async? it may be what you want...
0
 
leakim971PluritechnicianCommented:
oups I forget to add the data line 5 :


return $.ajax({ url: "some.php", data:d, async: false }).responseText;

Open in new window

0
 
sahanzAuthor Commented:
Cool, that's what I wanted :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.