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