程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在打字稿中依次进行一个或多个 ajax 调用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在打字稿中依次进行一个或多个 ajax 调用?

开发过程中遇到在打字稿中依次进行一个或多个 ajax 调用的问题如何解决?下面主要结合日常开发的经验,给出你关于在打字稿中依次进行一个或多个 ajax 调用的解决方法建议,希望对你解决在打字稿中依次进行一个或多个 ajax 调用有所启发或帮助;

我正在开发一个 ASP.NET Web Forms 项目,其中前端是使用 typescript 和 Jquery 开发的。

当前代码像这样进行顺序调用,

    private SearchByID() {

    $.when(DefaultComponent.Method5(),DefaultComponent.Method4())
        .always(function () {
            DefaultComponent.Method1();
        }).always(function () {
            DefaultComponent.Method3();
        })
        .always(function () {
            DefaultComponent.Method2();
        });
}

这些方法取决于其他方法的响应。 此外,可以根据用户的搜索类型以任何顺序调用这些方法。 例如,如果用户按名称搜索,则

    private SearchByname() {

    $.when(DefaultComponent.Method1(),DefaultComponent.Method2())
        .always(function () {
            DefaultComponent.Method5();
        }).always(function () {
            DefaultComponent.Method4();
        })
        .always(function () {
            DefaultComponent.Method3();
        });
}

方法内部的代码如下所示,

    private static Method1() {

    let deferred = $.Deferred();
    //data valIDation

    //create and pass request object
    let requestXhr = Default@R_801_9260@ce.getmethod1Data();

    $.when(requestXhr).done(function (response: String) {
        //handle response
        $("#divResult").append(responsE).append("<br />");
        deferred.resolve();
    }).fail(function () {
        //display error
        deferred.reject();
    }).always(function () {
        //display mossages based on respons in UI
    });

    return deferred.promise();
}

上面的方法现在起作用了。但是,我试通过使用使其更具可读性和简单性 与 Jquery AJAX 一起异步等待。

我尝试遵循 typescript 文档中的 async await 示例,但不确定如何继续。

来自打字稿文档的示例:

// Async/Await took code which looked like this:

// getResponse(url,(responsE) => {
//   getResponse(response.url,(secondResponsE) => {
//     const responseData = secondResponse.data
//     getResponse(responseData.url,(thirdResponsE) => {
//       ...
//     })
//   })
// })

// And let it become linear like:

// const response = await getResponse(url)
// const secondResponse = await getResponse(response.url)
// const responseData = secondResponse.data
// const thirdResponse = await getResponse(responseData.url)
// ...

// Which can make the code sit closer to left edge,and
// be read with a consistent rhythm.

任何建议/示例将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的在打字稿中依次进行一个或多个 ajax 调用全部内容,希望文章能够帮你解决在打字稿中依次进行一个或多个 ajax 调用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。