最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery : pass argument to ajax error function - Stack Overflow

programmeradmin2浏览0评论

I've been making some ajax function these days, and I'm facing a small problem. I have a function that calls an ajax. I want to give it a value, and return that function when the request is done. How do I either:

  • Trigger the return from the ajax sub functions
  • Wait for the "answer" var to change and then return it

Here's the spirit (indeed, it cannot work) :

var answer = null;
    $.ajax({
        url: "validate/"+id,
        type: 'POST',
        data: {'field' : value},
        success: function(data) {
            //noty({text: data, type: 'success'});
        },
        error:function (xhr, ajaxOptions){
            noty({text: xhr.status + " : " + xhr.responseText, type: 'error'});
            answer = "error";
        } 
    });

return answer;

Thank you!

I've been making some ajax function these days, and I'm facing a small problem. I have a function that calls an ajax. I want to give it a value, and return that function when the request is done. How do I either:

  • Trigger the return from the ajax sub functions
  • Wait for the "answer" var to change and then return it

Here's the spirit (indeed, it cannot work) :

var answer = null;
    $.ajax({
        url: "validate/"+id,
        type: 'POST',
        data: {'field' : value},
        success: function(data) {
            //noty({text: data, type: 'success'});
        },
        error:function (xhr, ajaxOptions){
            noty({text: xhr.status + " : " + xhr.responseText, type: 'error'});
            answer = "error";
        } 
    });

return answer;

Thank you!

Share Improve this question edited Jun 19, 2012 at 13:47 thecodeparadox 87.1k22 gold badges142 silver badges164 bronze badges asked Jun 19, 2012 at 13:44 Gabriel TheronGabriel Theron 1,3764 gold badges21 silver badges52 bronze badges 3
  • You make the function accept a callback, which is called inside the error handler and pass the answer value to it, like you do it with noty. You cannot return a value from an asynchronous function call. Or better yet: Use deferred objects: api.jquery./category/deferred-object – Felix Kling Commented Jun 19, 2012 at 13:49
  • Wow, that kind of way to do it is so strange to me... I'll try it – Gabriel Theron Commented Jun 19, 2012 at 13:50
  • Well, you can if you make a blocking call, but it would be unwise, as it blocks browser UI for the period of request – poncha Commented Jun 19, 2012 at 13:50
Add a ment  | 

2 Answers 2

Reset to default 4

You can't return values from an AJAX function, as an AJAX request happens asynchronously (think how long it takes to retrieve a remote webpage).

Instead you need to provide a callback (a function which is executed when the request pletes):

function ajaxFunction(onComplete) {
    $.ajax({
        url: "validate/"+id,
        type: 'POST',
        data: {'field' : value},
        success: function(data) {
            //noty({text: data, type: 'success'});
            onComplete(data);
        },
        error:function (xhr, ajaxOptions){
            noty({text: xhr.status + " : " + xhr.responseText, type: 'error'});
            onComplete(error);
        }
    }
}

You'd then pass a function as a parameter to ajaxFunction, which will receive the response from the AJAX request.

ajaxFunction(function (answer) {
    // do something with answer
});

As you need an id and value parameter, you could add those to arguments of the ajaxFunction method.

Use async: false flag - see the jQuery.ajax documentation for reference.

Note that in jQuery 1.8, the use of async will be deprecated.

var answer = null;

$.ajax({
    url: "validate/"+id,
    type: 'POST',
    data: {'field' : value},
    success: function(data) {
        //noty({text: data, type: 'success'});
    },
    error:function (xhr, ajaxOptions){
        noty({text: xhr.status + " : " + xhr.responseText, type: 'error'});
        answer = "error";
    },
    async: false
});

return answer;
发布评论

评论列表(0)

  1. 暂无评论