【问题标题】:AngularJS http post not workingAngularJS http帖子不起作用
【发布时间】:2016-09-08 05:27:31
【问题描述】:

我已经创建了一个服务并使用它来登录:

编辑:添加了“成功”和“错误”代码。

编辑 2: 我正在开发一个包含 Javascript/AngularJS 的 iOS 移动应用程序。那么有没有办法将错误视为警报..

.service('Login', function($http, $q, $httpParamSerializerJQLike) {
   return {
      loginUser: function(ipAdd, name, pw) {

         var sendurl = 'http://' + ipAdd + ':8080/loginuser/user.cgi';

         var postData = {
            'ACTION' : 'login',
            'LOGIN' : name,
            'PASSWORD' : pw
         }; 

        //what is the mistake here?
         return $http({
                      method : 'POST',
                      url : sendurl,
                      data : $httpParamSerializerJQLike(postData),
                      headers : { 'Content-Type': 'application/x-www-form-urlencoded'}
                      }).success(function(response) {
                         var x2js = new X2JS();
                         var jsonObj = x2js.xml_str2json(response.data);

                         if (typeof jsonObj === 'object') {
                             alert("here:1");
                             return jsonObj;
                         } else {
                             alert("here:2");
                             // invalid response
                             return $q.reject(jsonObj);
                         }

                      }).error(function(response) {  
                         //do error
                         //comes here when no internet connection is found..
                           alert("here:3");
                           return $q.reject(response.data);
                      });

         }
      }
   })

我也在 app.js 中包含了这个:

var app = angular.module("myApp", ['starter.services'],function($httpProvider){
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
});

我的实际网址如下所示:

'http://' + ipAdd + ':8080/loginuser/user.cgi?ACTION=login&LOGIN=' + name + '&PASSWORD=' + pw;

我也尝试过这种方法:https://*.com/a/25570077/5876598

我的服务没有返回任何东西.. 我想知道我是否在我的 url 格式中或在发送数据时出错。

谢谢。

【问题讨论】:

  • 检查控制台中的错误
  • 尝试 data:postData 没有 $httpParamSerializerJQLike 并检查控制台是否有错误
  • $http 返回promise。你期待什么?您的成功回调为空。
  • 我没有发布整个代码@StepanKasyanenko
  • 显示调用loginUser函数。控制台有错误?

标签: javascript angularjs http post http-post


【解决方案1】:

了解错误出处的最佳方法是检查导航器开发人员控制台上的“网络”选项卡。

假设您在 linux 或 mac 上运行,您还可以尝试使用 CURL 来了解您尝试访问的 url 的返回值。

我们不能只为您提供代码。

【讨论】:

    【解决方案2】:

    我在服务中添加了延期承诺。

    我还将 "success().error()" 更改为 ".then(function(data, status, headers, config){}) 。不知道为什么我用了success和error的时候还是不行。

    【讨论】:

      【解决方案3】:

      实际上,之前我在服务中提供承诺时发现了一些问题。遵循以下结构

      .service('Login', function($http, $q, $httpParamSerializerJQLike) {
        return {
         loginUser: function(ipAdd, name, pw) {
      
           var sendurl = 'http://' + ipAdd + ':8080/loginuser/user.cgi';
      
           var postData = {
              'ACTION' : 'login',
              'LOGIN' : name,
              'PASSWORD' : pw
           }; 
      
          //what is the mistake here?
           return $http({
                        method : 'POST',
                        url : sendurl,
                        data : $httpParamSerializerJQLike(postData),
                        headers : { 'Content-Type': 'application/x-www-form-urlencoded'}
                        });
      
           }
        }
      })
      
      .controller('SomeCtrlName',function(Login){
          //pass your parameter to below service.
          Login.loginUser().then(function success(){
          //write success code here 
         },function error(){
          //write error code here
         )};
      })
      

      【讨论】:

        最近更新 更多