Angular $ http.post请求无效

时间:2016-01-22 10:32:21

标签: angularjs

我有一家工厂如下:

MyApp.factory('DB_Services', function($http , $q) {
    var l_Result ;
    var DB_Services = function(p_Query) {
                            var l_deferred = $q.defer();
                            var l_url      = "http://MyWeb.com:8080/Services/Dispatcher?";
                            var l_params   = '{"data":' + JSON.stringify(p_Query) + '}' ;

                            console.log("DB_Services - l_params: " + l_params ) ;

                            $http.post( {
                                               url:l_url,
                                               method:"POST",
                                               headers: {
                                                          'Content-Type': 'application/x-www-form-urlencoded'
                                               },
                                               data: l_params
                                          }
                            ).
                                        success(function(data, status, headers, config) {
                                             l_deferred.resolve(data);
                                        }).
                                        error(function(data, status, headers, config) {
                                            console.log("DB_Services - Error: " + data) ;
                                             l_deferred.reject(status);
                                        });
                                         return l_deferred.promise;

                        } ;

return DB_Services;
});

调用DB_Services时,我的浏览器(Chrome)在CONSOLE中显示以下内容:

1)请求数据为:

DB_Services - l_params: {"data":{"Action":"Get_Landing_Page_Messages","Request_Details":{"Language_Code":"EN"}}}

2)我将错误添加到错误中:​​

DB_Services - Error: Cannot POST /[object%20Object]

3)以下附加错误:

POST http://127.0.0.1:55402/[object%20Object] 404 (Not Found)

我为data尝试了几种可能的语法,但都没有。

有一个servlet http://MyWeb.com:8080/Services/Dispatcher可以处理GET和POST请求。当我通过在Chrome的地址栏中输入类似的数据字符串来发出GET请求时,我会按预期获得结果。

我无法理解问题所在。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

post方法的第一个参数是一个URL,第二个是带参数的对象,所以这两个都应该有效,具体取决于你的后端:

Sub excel_to_powerpoint()     
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Set PPApp = New PowerPoint.Application
PPApp.Visible = True
PPApp.Activate 
Set PPPres = PPApp.Presentations.Add
Set PPSlide = PPPres.Slides.Add(1, ppLayoutBlank)
Sheet2.Range("A1:C5").Copy
For i = 1 To 50000: DoEvents: Next
PPSlide.Shapes.PasteSpecial ppPasteDefault
Set myshape = PPSlide.Shapes(PPSlide.Shapes.Count)
myshape.Left = 50
myshape.Top = 50
Application.CutCopyMode = False
End Sub

见这里:https://docs.angularjs.org/api/ng/service/ $ http