0

My jquery method WILL not return value "Selected". My alert shows that the value is retrived from database. But if I write data=GetSelected(); it is undefined...

 function GetSelected()
 {
     $.ajax({
         type: 'GET',
         url: '/api/machine/',
         dataType: "JSON",
         data: "data",
         error: function (error) { },
         success: function (data)
         {
            $.each(data, function (i, data)
            {
                 if (data.machinenumber == 2)
                {       
                     selected = data.selected;
                     alert('Selected er: ' + selected)
                     return selected;
                 };

            });

         } 
     }); 
 }

1 Answers1

0

The return statement belongs to the function(i, data) header in the for-each loop, not the function (data) header that is getting executed on success. Basically the values gets returns to the outer function and ignored there.

Henning Koehler
  • 2,456
  • 1
  • 16
  • 20