AngularJs $http.post 數據後臺獲取不到數據問題 的解決過程

   第一次使用 AngularJs 的 $http 模塊的時候,遇到事後臺獲取不到前臺提交數據的問題,檢查代碼沒有發現問題,先上代碼。jquery

  js 代碼 json

angular.module("newsApp", [])
    .constant("newsInfoUrl", "/WebPage/Page/NewsInfo/")
    .factory("newsService", function($http) {
        return {
            getNewsList: function (categoryId, callBack) {
                //請求後臺數據 
                $http.post("/WebPage/Page/GetNewsList",
                    //參數分類ID,後臺獲取不到
                    { id: categoryId }
                    ).then(function (resp) {
                    callBack(resp);
                });
            } 
        }
    }) 
    .controller("newsListCtrl", [
        "$scope", "newsService", "newsInfoUrl", function($scope, newService, newsInfoUrl) {
            $scope.cId = "";
            var getNewsList = function() {
                newService.getNewsList($scope.cId, function(resp) {
                    $scope.newsList = resp.data;
                });
            }
            $scope.newsInfoUrl = newsInfoUrl;
            $scope.reload = getNewsList;
        }
    ]);
    

後臺代碼app

        [HttpPost]
        public JsonResult GetNewsList(FormCollection collection)
        { 
       //在這裏 collection 裏面沒有數據
var catrgoryId = collection["id"]; var page = new PageContext { PageSize = 20 }; var cList = new ContentBusiness().GetContentList(string.Empty, catrgoryId, page); return Json(ConvertModel(cList)); }

奇怪了,難道提交數據有問題?抓包看看ide

 

原來問題出在這裏,咱們平時用 jquery post 提交數據是以 form-data 的形式提交的,而 AngularJs 以 json 格式提交的,因此後臺獲取不到了。post

問題找到了,解決就容易了。url

 

解決方法 <一>  改後臺,以參數的形式接收,不使用 FormCollection 或 Request.Form[] spa

        [HttpPost]
        public JsonResult GetNewsList(string id)
        { 
            var page = new PageContext
            {
                PageSize = 20
            };
            var cList = new ContentBusiness().GetContentList(string.Empty, id, page);
            return Json(ConvertModel(cList));
        }

若是參數比較多,能夠定義一個model對象,model對象的屬性對應前臺提交的參數,以model對象做爲後臺響應方法的參數。code

解決方法 <二>  改AngularJs 提交數據的方式,使用 全局配置 配置$httpProvider 的 header 值,使用 transformRequest orm

          對提交數據進行序列化,把 json 對象更改成字符串。對象

     angular.module("newsApp", [])
     .config(["$httpProvider", function ($httpProvider) {
     //更改 Content-Type $httpProvider.defaults.headers.post[
"Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"; $httpProvider.defaults.headers.post["Accept"] = "*/*"; $httpProvider.defaults.transformRequest = function (data) { //把JSON數據轉換成字符串形式 if (data !== undefined) { return $.param(data); } return data; }; }]) .constant("newsInfoUrl", "/WebPage/Page/NewsInfo/") .factory("newsService", function ($http) { return { getNewsList: function (categoryId, callBack) { $http.post("/WebPage/Page/GetNewsList", {id: categoryId} ).then(function (resp) { callBack(resp); }); } } }) .controller("newsListCtrl", [ "$scope", "newsService", "newsInfoUrl", function($scope, newService, newsInfoUrl) { $scope.cId = ""; var getNewsList = function() { newService.getNewsList($scope.cId, function(resp) { $scope.newsList = resp.data; }); } $scope.newsInfoUrl = newsInfoUrl; $scope.reload = getNewsList; } ]);
相關文章
相關標籤/搜索