0

Possible Duplicate:
jQuery ajax return value
How to return the response from an AJAX call from a function?

I have javascript. It loads data from database. I want to return true or false with respect to loading data. But I could not return it. My code has given bellow:

function CheckISRC() {
     var url = "/TrackEdit/CheckISRC/" + $('#isrcid').val();
     var isrc = $('#isrcid').val();
     var result = false;
     $.get(url, {
         isrc: isrc
     }, function (data) {
         if (data == "true") {
             result = true;
         }
         else {
             result = false;
         }
     });
     return result;
 }

It always gives false result. Anyone has faced this kind of problem? 'Thanks advance'

Community
  • 1
  • 1
user1928185
  • 112
  • 1
  • 11

3 Answers3

0

If it's so important to use the function synchronously you can refactor it to:

function CheckISRC() {
     var url = "/TrackEdit/CheckISRC/" + $('#isrcid').val();
     var isrc = $('#isrcid').val();
     var result = false;
     $.ajax({
        async: false,
        success: function (data) {
           if (data == "true") {
               result = true;
           }
           else {
               result = false;
           }
         },
         data: { isrc: isrc }
     });
     return result;
 }

As @ManseUK async is deprecated in jQuery 1.8 so if you want synchronous approach you should use older version.

Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
  • `async: false` is deprecated as of jQuery 1.8 – Manse Jan 21 '13 at 13:56
  • Ok, then he should use previous version :-) – Minko Gechev Jan 21 '13 at 13:57
  • **Nope** the OP should use the function properly and use a callback – Manse Jan 21 '13 at 13:57
  • He should but I cant force him to do it. – Minko Gechev Jan 21 '13 at 13:58
  • @Minko Gechev. Thank you very much for your answer. I need to define URL. I tried your answer. But it does not call back end function. – user1928185 Jan 21 '13 at 14:12
  • 2
    `deprecated` does not mean it's removed. And to be correct, only the use of `async: false` together with deferred objects is deprecated, at least that's how I understand the documentation. But that does not change the fact the making synchronous calls is a really bad idea. – Felix Kling Jan 21 '13 at 14:21
  • @FelixKling absolutely agree. – Minko Gechev Jan 21 '13 at 14:22
  • @MinkoGechev yet your answer still suggests using it even though you agree with *making synchronous calls is a really bad idea* – Manse Jan 21 '13 at 14:24
  • I suggest the simplest answer which won't require refactoring. Just question -> answer. I haven't told "I recommend..." or "You must..."...My answer is "If it's so important to use the function synchronously..." which in my opinion doesn't suggest to use ajax in that way. – Minko Gechev Jan 21 '13 at 14:27
0

The JQuery ajax functions are asynchronous. This means that when you initialise result to false, the result is set to true or false after the "return result;" line has run.

You can make the call synchronous but this is considered worse practice. You are often better off refactoring your code to allow for the asynchronous nature of the JQuery Ajax.

For example, where you previously had:

function myFunction() {
    //Code before
    var result = CheckISRC();
    //Code after using result
}

you could have the following:

function myFunction() {
    //Code before
    CheckISRC();
}
function myFunction_callback(result) {
    //Code after using result
}

where you call myFunction_callback in the success option of your ajax code like so:

function CheckISRC() {
     var url = "/TrackEdit/CheckISRC/" + $('#isrcid').val();
     var isrc = $('#isrcid').val();
     $.get(url, {
         isrc: isrc
     }, function (data) {
             myFunction_callback(data == "true");
     });
 }
jaypeagi
  • 3,133
  • 18
  • 33
  • [jaypeagi] thank you for your reply. It would be work. I have one more problem. It gives result after from submission. But I call it before from submission. I need result before submssion – user1928185 Jan 21 '13 at 15:38
  • If you are calling `CheckISRC` before a form submits, you will have to somehow cancel the submission before calling it, then trigger the submission manually in the callback function. – jaypeagi Jan 22 '13 at 09:52
  • It may be best for your code to fire in an `onclick` event rather than on submit. Then you could submit the form manually in JavaScript. Check out JQuerys submit function: http://api.jquery.com/submit/ – jaypeagi Jan 22 '13 at 09:53
  • thank you very much. Finally I solved it by focusout. – user1928185 Jan 22 '13 at 13:20
  • Glad to have helped. Please mark as answer if it solved your problem. Thanks. – jaypeagi Jan 22 '13 at 13:48
0

The problem is that when you return result, It doesnt have value. because the ajax didn't finish its task. you make some callback function and when the result of ajax is returned from server, do what you want to.

Some thing like this:

function CheckISRC(Callback) {
    var url = "/TrackEdit/CheckISRC/" + $('#isrcid').val();
    var isrc = $('#isrcid').val();
    var result = false;
    $.get(url, {
        isrc: isrc
    }, function (data) {
        if (data == "true") {
            Callback(true);
        }
        else {
            Callback(false);
        }
    });
 }
 function YourCallback(result) {
     //...
 }
Hosein
  • 581
  • 1
  • 7
  • 29