Angularjs 将 Ajax 请求与服务中的承诺联系起来的问题 [英] Angularjs issue in relacing Ajax request with promises in service

查看:39
本文介绍了Angularjs 将 Ajax 请求与服务中的承诺联系起来的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于服务中的 Angularjs 应用程序,我使用 Ajax 调用来获取数据,如下所示:

For my Angularjs application in services I have used Ajax call to get the data and is as follows :

var originalRequest = $.ajax({
                    async : false,
                    url : "/dash/dashboard2ajax.do",
                    type : "POST",
                    data : {
                        action : 'getNetSpendOverTime',
                        customerId : selectedAccTd,
                        carriersId : selectedCarriers,
                        fromDate : formattedFromDate,
                        toDate : formattedToDate
                    },
                    dataType : "json",
                    success : function(originalRequest) {
                        var res = originalRequest;
                        data = res.ResultSet.Response;

                    }
                });

然后我只是从我的服务和我的控制器中返回(数据),我能够毫无问题地获取数据.但我意识到这是一个不好的做法,并试图使用承诺.所以我把它替换如下:

Then I just return (data) from my service and in my controller I am able to get data without any problem. But I realized it is a bad practice and trying to use promises. So I have replaced it as follows:

var originalRequest = $http({
            url: "/dash/dashboard2ajax.do",
            method: "POST",
            data: {action : 'getNetSpendOverTime',
                customerId : selectedAccTd,
                carriersId : selectedCarriers,
                fromDate : formattedFromDate,
                toDate : formattedToDate}
        }).success(function(data, status, headers, config) {
           return (data);
        }).error(function(data, status, headers, config) {
            return(status);
        });

但是它不起作用.没有一个参数被传递到我的动作类.我的语法有错误吗?

But it is not working. None of the parameters are getting even passed to my action class. Is there any mistake in my syntax?

在我的操作类中,我访问参数为

In my action class, I am accessing the parameters as

String action = request.getParameter("action");

但它是空的.

推荐答案

您正在尝试用 AngularJS $http 替换 jQuery.ajax,后者具有完全不同的契约.您所称的 originalRequest 实际上不是任何类型的请求对象".它只是一个 Promise(扩展了 successerror 方法).如果你想在你的成功/错误处理程序之外访问请求数据,你需要自己单独保存和传递:

You're trying to replace jQuery.ajax with AngularJS $http, which has completely different contract. The thing you're calling originalRequest is not in fact any kind of "request object". It's just a Promise (extended with success and error methods). If you want to access the request data outside your success/error handlers, you need to save and pass it separately yourself:

var data = {
    // ...
};
var request = $http({
    data: data,
    // ...
});
return {
    request: request,
    data: data
};

如果您需要在处理程序中访问它,只需从 config 参数中获取它:

If you need to access it inside the handlers, just get it from the config argument:

$http(...).success(function (data, status, headers, config) {
    var action = config.data.action;
    // ...
});

这篇关于Angularjs 将 Ajax 请求与服务中的承诺联系起来的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆