AngularJs $http. Post data background Get no data problem resolution process

Source: Internet
Author: User

The first time to use the AngularJs $http module, encountered in the background can not get the foreground submit data problems, check the code did not find the problem, first on the code.

JS Code

Angular.module ("newsApp", []). Constant ("Newsinfourl", "/webpage/page/newsinfo/"). Factory ("Newsservice",function($http) {return{getnewslist:function(CategoryId, callBack) {//Request background Data$http. Post ("/webpage/page/getnewslist",                    //parameter category ID, not available in background{Id:categoryid}). Then (function(RESP) {callBack (RESP);            }); }         }    }) . Controller ("Newslistctrl", [        "$scope", "Newsservice", "Newsinfourl",function($scope, NewService, Newsinfourl) {$scope. cId= ""; varGetnewslist =function() {newservice.getnewslist ($scope. CId,function(resp) {$scope. newslist=Resp.data;            }); } $scope. Newsinfourl=Newsinfourl; $scope. Reload=getnewslist;    }    ]); 

Background code

[HttpPost] PublicJsonresult Getnewslist ( formcollection Collection) {
       //There is no data in this collection varCatrgoryid = collection["ID"]; varpage =NewPageContext {PageSize= - }; varCList =NewContentbusiness (). Getcontentlist (string. Empty, Catrgoryid, page); returnJson (Convertmodel (cList)); }

Strange, is there a problem with submitting the data? Grab a bag and see

The original problem here, we usually use jquery post submission data is submitted in the form of Form-data, and AngularJs submitted in JSON format, so the background is not available.

The problem was found and the solution was easy.

workaround < a >  change the background, receive as a parameter, do not use formcollection or request.form[]

        [HttpPost]        public jsonresult getnewslist (string  id)        {                varNew  pagecontext            {                $            };             var New Contentbusiness (). Getcontentlist (string. Empty, ID, page);             return Json (Convertmodel (cList));        }

If the parameters are more, you can define a model object that has properties for the foreground submission, and a model object as a parameter to the background response method.

Workaround < two > change ANGULARJS to submit data, use global configuration to configure $httpprovider header value, use transformrequest

Serializes the commit data and changes the JSON object to a string.

Angular.module ("newsApp", []). config (["$httpProvider",function($httpProvider) {
     // change 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) {//convert JSON data into string form if(Data!==undefined) { return$.param (data); } returndata; }; }]). 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= ""; varGetnewslist =function() {newservice.getnewslist ($scope. CId,function(resp) {$scope. newslist=Resp.data; }); } $scope. Newsinfourl=Newsinfourl; $scope. Reload=getnewslist; } ]);

AngularJs $http. Post data background Get no data problem resolution process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.