简体   繁体   English

Angular JS中的HTTP Post请求

[英]Http Post request in Angular JS

We are new to angular js. 我们是angular js的新手。

We tried http request using $http.get it is working fine. 我们使用$http.get尝试了http请求,它工作正常。 But in post request it is creating an issue, it comes to success and show bad parameter error code 103. 但是在后期请求中却造成了一个问题,成功并显示错误的参数错误代码103。

Same we have tried in ajax request using $.ajax({}), and it is working fine. 我们在使用$.ajax({}), ajax请求中尝试过的方法也一样$.ajax({}),并且工作正常。

I have also paste my code. 我也粘贴了我的代码。

Can anyone help us out? 谁能帮助我们?

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

You can use http Post in following way: 您可以通过以下方式使用http Post:

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

The name of the http method should be written in uppercase. http方法的名称应大写。 Also, the property datatype is not awaited by $http , you should remove it: 另外, $http不等待属性datatype ,您应该将其删除:

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

Note, in the above call to $http you are setting the header 'Content-Type': 'application/json' . 注意,在上面对$http调用中,您设置了标头'Content-Type': 'application/json' But this header is automatically injected by $http ( see $http documentation ), therefore you can remove it, and use the short syntax: 但是此标头是由$http自动注入的( 请参阅$ http文档 ),因此您可以删除它,并使用短语法:

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

with data equals to: data等于:

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

Are You Getting this error ?? 您是否收到此错误?

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

If Yes, Its Not your Problem Contact the Web service provider. 如果是,则不是您的问题,请与Web服务提供商联系。

Ask him to give the valid parameter 请他提供有效参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM