簡體   English   中英

Angular JS中的HTTP Post請求

[英]Http Post request in Angular JS

我們是angular js的新手。

我們使用$http.get嘗試了http請求,它工作正常。 但是在后期請求中卻造成了一個問題,成功並顯示錯誤的參數錯誤代碼103。

我們在使用$.ajax({}), 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));
        });
    };
});

您可以通過以下方式使用http Post:

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

http方法的名稱應大寫。 另外, $http不等待屬性datatype ,您應該將其刪除:

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文檔 ),因此您可以刪除它,並使用短語法:

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

data等於:

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

您是否收到此錯誤?

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

如果是,則不是您的問題,請與Web服務提供商聯系。

請他提供有效參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM