0

Im trying to return the value that a $ajax call returns, from a function but it only returns "undefined". If a alert the "reponse" from the ajax call it returns the rigth value. Here is the code, what am i doing wrong?:

$(".insertCandidate").live("click", (function(e) {
            var ids = this.id.toString().split("|");
            var tempCanID = ids[1];
            var canID = ids[0];
            var tempName = CandidateName(tempCanID);
            var canName = CandidateName(canID);
            //alert("HTML: "+tempName);
            $("#mergeCandidateDialog").empty();
            $.blockUI({ message: $("#mergeCandidateDialog").append(
                "<div>" + tempName + "s ansøgning til vil blive lagt under den eksiterende ansøger s data.<br /><br /> Ønsker du at fortsætte?<br /><br /></div>" +
                "<div id=\"content\">" +
                "<input type=\"button\" id=\"" + ids + "\" class=\"insertCandidateYes\" value=\"Ja\" />" +
                "<input type=\"button\" id=\"insertCandidateNo\" value=\"Nej\" /></div>"), css: { cursor: 'default', fontWeight: 'normal', padding: '7px', textAlign: 'left' }
            });
        }));

function CandidateName(candidateID) {
        var returnstring;
        $.ajax({
            type: "POST",
            url: "/Admin/GetCandidateName/",
            data: { 'candidateID': candidateID },
            succes: function(response) {
                returnstring = response;
                return;
            },
            error: function(response) {
                alert("FEJL: " + response);
            }
        });

        return returnstring;
    }

4 Answers 4

3

You cannot do this unless you wait for the ajax call to complete by using asynch:false. However I would not do this as it locks up the browser and if the call fails to return the user will have to crash out. It is better to refactor your script and within the success function of the .ajax call to invoke a callback function.

See my answer to a similar question here

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

Comments

1

Add an async true attribute to your .ajax call:

    type: "POST",
    async: false, // ADD THIS
    url: "/Admin/GetCandidateName/",

Also, you left off an "s" on success.

That may do it.

1 Comment

asynch is true by default, not sure how this helps?
0

You're doing 2 ajax requests which are identical. To improve performance you should pass an array of IDs to your CandidateName method and then your server-side script also should return an array of matched names.

Comments

-2
 $.ajax({
        type: "POST",
        url: "/Admin/GetCandidateName/",
        data: { 'candidateID': candidateID },
        success: function(response) {
            return response;//USE THIS

            //THIS WILL RETURN FROM THE FUNCTION WITHOUT RETURNING ANY VALUE
            //return;
        },

2 Comments

well, i need the ajax call to be in a function and when the script use posted is in a function, that doesn't work either. I tried it.
The success function (if you spell the name correctly) is called in response to an event firing. It isn't called by the function that called the $.ajax function. (What is $ here anyway? I don't recognize the pattern. blog.dorward.me.uk/2009/02/19/the-dollar-function-must-die.html )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.