2015-09-16 87 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服務提供商。

請求他給出有效參數