在relacing Ajax请求,在服务承诺Angularjs问题 [英] Angularjs issue in relacing Ajax request with promises in service

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

问题描述

有关我的服务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");

但它是未来为空。

But it is coming as null.

推荐答案

您是要替换 jQuery.ajax 与AngularJS $ HTTP ,它具有完全不同的合同。你打电话 originalRequest 的事情其实也不是任何类型的请求对象的。这只是一个无极(扩展了成功错误方法)。如果你想访问你的成功/错误处理程序之外的请求数据,您需要保存,并分别通过它自己:

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
};

如果您需要访问它的内部处理,从配置参数只是得到它:

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;
    // ...
});

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

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