1

In my controller have actionGetRandomCode. Why when I shown with jQuery like this

var ran = $.get('index.php?r=registrations/get-random-code');
alert(ran);

Its getting show [object Object]?
But in my dev tool response I get what I want RQLGLS

Andreas
  • 21,535
  • 7
  • 47
  • 56
Stfvns
  • 1,001
  • 5
  • 16
  • 42
  • 1
    [`$.get()`](http://api.jquery.com/jquery.get/) + [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call?rq=1) – Andreas Nov 07 '15 at 18:01
  • alert should be forbidden in javascript – Daimos Nov 07 '15 at 18:09
  • not just alert. when shown to table `$('#sampleTbl tbody').append('' + ran + '');` Its show `[object object]` too – Stfvns Nov 07 '15 at 18:12
  • 1
    You need to use .done and put the alert inside it. The Ajax methods are asynchronous so you need to use a callback function to resolve the result. Read up on promises – vbranden Nov 07 '15 at 18:15
  • thank its work `var ran = $.get('index.php?r=registrations/get-random-code').done(function(data) { alert( "Data: " + data ); });` @vbranden – Stfvns Nov 07 '15 at 18:22
  • Please get urself familiar to use console.log().Its developer's friendly. At least for me. – Nizam Nov 20 '15 at 08:01

1 Answers1

0

You need to use .done and put the alert inside it. The Ajax methods are asynchronous so you need to use a callback function to resolve the result

var ran = $.get('index.php?r=registrations/get-random-code').done(function(data) {
    alert( "Data: " + data );
});
Stfvns
  • 1,001
  • 5
  • 16
  • 42