weixin_33713503 2019-04-25 14:21 采纳率: 0%
浏览 49

识别AJAX请求

I am triggering multiple AJAX requests in a loop. They run in parallel and it is not clear which one will respond first. If the response is successful, I can identify the request by analyzing the response.

for (kk = 0; kk < $('#style').val().length; kk++){
    $.ajax({
        type: "POST",
        url: "/single",
        data: {style: [$('#style').val()[kk]]},
        success: function (results) {
            if (results.status == 'success'){
                $('#results').find('div').each(function(){
                    if ($(this).attr('id') == results.style){
                        $(this).empty().append(results.payload)
                    }
                });
            }
            else{
                $('#results').find('div').each(function(){
                    if ($(this).attr('id') == results.style){
                        $(this).empty().append('<b>' + results.style + ':</b> ' + results.payload)
                    }
                });
            }
        },
        error: function (error) {

            console.log(error);
        }
    });
}

However, once in a while, the request fails and an error is triggered. For a proper error handling, I would like to know to which of the (previously triggered) requests the error belongs.

Is there a clean method how a specific AJAX request can be identified?

  • 写回答

1条回答 默认 最新

  • csdnceshi62 2019-04-25 14:33
    关注

    I would recommend to pass in a identifier via context to the AJAX call which you can use inside the success or error methods:

    for (kk = 0; kk < $('#style').val().length; kk++){
        $.ajax({
            type: "POST",
            url: "/single",
            data: {style: [$('#style').val()[kk]]},
            // data inside "context" will be available as part of "this" in the success/error case. 
            context: {
              "kk": kk
            },
            success: function (results) {
                if (results.status == 'success'){
                    console.log("Element " + this.kk + " finished successfully.");
                    $('#results').find('div').each(function(){
                        if ($(this).attr('id') == results.style){
                            $(this).empty().append(results.payload)
                        }
                    });
                }
                else{
                    $('#results').find('div').each(function(){
                        if ($(this).attr('id') == results.style){
                            $(this).empty().append('<b>' + results.style + ':</b> ' + results.payload)
                        }
                    });
                }
            },
            error: function (error) {
                console.log("Element " + this.kk + "failed.");
                console.log(error);
            }
        });
    }
    

    More information regarding context can be found in the jQuery documentation.

    Regarding your comment about checking how many calls failed / succeeded: here is a JsFiddle demonstrating how to keep track of the call statistics.

    评论

    报告相同问题?

    悬赏问题

    • ¥15 存储过程或函数中的结果集类型变量如何使用。
    • ¥80 关于海信电视聚好看安装应用的问题
    • ¥15 vue引入sdk后的回调问题
    • ¥15 求一个智能家居控制的代码
    • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
    • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
    • ¥20 求各位能用我能理解的话回答超级简单的一些问题
    • ¥15 yolov5双目识别输出坐标代码报错
    • ¥15 这个代码有什么语法错误
    • ¥15 给予STM32按键中断与串口通信