0

I am trying to verify an id using javascript ajax.

An onclick event triggers the ajax call from within a function:

verified = verify_id($('#set_ex_id').val());
alert('Verified: ' + verified);

The verify_id function, upon success has this:

result = data;
alert("RESULT 1: " + result);
return result;

The alert Result 1 is 1, but the alert Verified is undefined

Judson
  • 97
  • 7
  • You question indicates that you are making an AJAX call, but the code you posted doesn't make any such call. Did you forget to include that part of the code? – dgvid Mar 20 '14 at 17:55
  • I only supplied the parts I thought may be in question. yes, it is using ajax, which is the second snippet and is working. – Judson Mar 20 '14 at 18:01

1 Answers1

0

If the ajax call is made on the verify_id function, then the value won't by assigned to verified inmediately. Any code that needs the return value from verify_id to execute should be placed in the success function of the ajax call.

You may set the async propertie of the ajax call to false, this way the execution will stop until the value arrives from the server, and you'll get the value you need in the alert('Verified: ' + verified); call. But this will also block the page untill the server response is ready.

user3417400
  • 341
  • 2
  • 4
  • Thanks. What ended up doing is adding a section in the ajax success response to handle the response. – Judson Mar 20 '14 at 19:24