2015-09-16 164 views
1

我们对angular js很陌生。Angular JS中的Http Post请求

我们尝试使用http请求$http.get它工作正常。但在后期要求它是创建一个问题,它涉及到成功和显示错误的参数错误代码103.

我们已经尝试了使用$.ajax({}),的AJAX请求,它工作正常。

我也粘贴我的代码。

任何人都可以帮助我们吗?

mainApp.controller('registrationCtrl', function($scope, $location, $http) { 
    $scope.registration = function() { 
     console.log("registration called"); 
     var ws_url = "http://apparelinindia.com/selfiestandoff/WebServices/register"; 

     var request = $http({ 
      method: "post", 
      url: ws_url, 
      data: { 
       user_email: $scope.email, 
       user_password: $scope.password 
      }, 
      dataType: 'json', 
      headers: { 
       'Content-Type': 'application/json' 
      } 

     }); 
     request.success(function(data) { 
      console.log("Success" + data); 
     }); 
     request.error(function(error) { 
      console.log("Success" + JSON.stringify(error)); 
     }); 
    }; 
}); 
+0

尝试用'method:“POST”'而不是? –

+0

并移除'dataType:'json'' –

回答

1

您可以按以下方式使用HTTP POST:

var request = $http.post(ws_url, { 
     user_email: $scope.email, 
     user_password: $scope.password 
    }); 
1

HTTP方法的名称应以大写字母写。此外,物业datatype没有被$http期待已久的,你应该将其删除:

var request = $http({ 
    method: "POST", 
    url: ws_url, 
    data: { 
     user_email: $scope.email, 
     user_password: $scope.password 
    }, 
    headers: { 
     'Content-Type': 'application/json' 
    } 
}); 

注意,在上面的调用$http要设置页眉'Content-Type': 'application/json'。但是,这头自动注射$http$http documentation),因此你可以将其删除,并使用短语法:

var request = $http.post(ws_url, data); 

data等于:

{ 
    user_email: $scope.email, 
    user_password: $scope.password 
} 
0

你得到这个错误?

{"status":false,"error":{"code":"103","message":"Required Parameters not found"}} 

如果是,它不是您的问题联系Web服务提供商。

请求他给出有效参数