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'

5
  • Have you checked whether the response from the database is equal to "true"? You can do this via Firebug, Chrome Developer Tool, or IE Developer Tool. Commented Jan 21, 2013 at 13:55
  • Thats because .get is asynchronous by default - you should read about async and callbacks. Commented Jan 21, 2013 at 13:55
  • 1
    Anyone has faced this kind of problem? at least 3 people every day ;) Commented Jan 21, 2013 at 14:18
  • @FelixKling perfect link ... that should be the reference for these questions Commented Jan 21, 2013 at 14:22
  • @ManseUK: Thanks. That's why I created it ;) Commented Jan 21, 2013 at 14:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

async: false is deprecated as of jQuery 1.8
Ok, then he should use previous version :-)
Nope the OP should use the function properly and use a callback
He should but I cant force him to do it.
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.
|
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) {
     //...
 }

Comments

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");
     });
 }

5 Comments

[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
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.
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: api.jquery.com/submit
thank you very much. Finally I solved it by focusout.
Glad to have helped. Please mark as answer if it solved your problem. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.