JsonResult用法请教 angular

baidu_35028162 2016-05-18 11:42:32
使用angular返回json数据,页面一直显示不出来数据
js
[CODE=C#
app.controller('safeCtrl', ['$scope', 'test', function ($scope, test) {
test.getjson().then(function (response) {
$scope.rowCollection = response.data;
}, function (error) {
console.log(error);
});
}]);
app.factory('test', [
'$http', '$location', function ($http, $location) {
var baseurl = $location.absUrl();
var factory = {};

factory.getjson = function () {
return $http({ cache: true, url: baseurl + "/Home/getjson", method: 'GET' });
}
return factory;
}
]);
/CODE]
后台代码
[CODE=C#
public JsonResult getjson()
{
DataClasses1DataContext a =new DataClasses1DataContext();
var player=from b in a.football select b;

var players = player.ToList();


return Json(players,JsonRequestBehavior.AllowGet);

}
/CODE]
试验在数据库加了2条数据,显示不出来,帮忙看看这样子写为什么不行,或者是写错了有什么更好的办法。搞了一天,js代码换了无数次实在是没辙了,求帮忙
...全文
231 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_35028162 2016-05-19
  • 打赏
  • 举报
回复
改成这样也不行 app.controller('safeCtrl', ['$scope', function ($scope,$http) { $http.get("/Home/getjson/"). success(function (data, status) { $scope.rowCollection = data; }).error(function (data, status) { $scope.alerts.push(data); }); }]);
範先森 2016-05-19
  • 打赏
  • 举报
回复
首先确定你后台那边返回的值是否正确,如果正确的话,就在JS中找错误
baidu_35028162 2016-05-19
  • 打赏
  • 举报
回复
引用 6 楼 starfd 的回复:
F12确认你的ajax请求返回的内容正确与否,确认到底是后台错了,还是前端问题
我是想用angular来实现
  • 打赏
  • 举报
回复
引用 7 楼 baidu_35028162 的回复:
[quote=引用 6 楼 starfd 的回复:] F12确认你的ajax请求返回的内容正确与否,确认到底是后台错了,还是前端问题
我是想用angular来实现[/quote] 这个回复,跟你写“js代码换了无数次实在是没辙了”如出一辙。 你要调试 http 消息,来判断是否真正给浏览器端返回了数据。你可能没有理解。
  • 打赏
  • 举报
回复
F12确认你的ajax请求返回的内容正确与否,确认到底是后台错了,还是前端问题
Xanl 2016-05-19
  • 打赏
  • 举报
回复
controller:

angular.module('controllers', [])
.controller('login-controller',function($scope,authSvc,testSvc){
	$scope.testData="aaaaaacc";
	$scope.passportData={};
	$scope.logindata=function(){
		var formData={
			json:{
				usrName:this.usrName,
				pwd:this.pwd
			}
		};;

		//$scope.testData=authSvc.login(formData);
		authSvc.httptest(formData,logincallback);
		//alert(aa);
	}

	$scope.btnClick=function(){
		alert('im clicked');
	}
})

function logincallback(responseData){
	alert(JSON.stringify(responseData)+'callback')
}
Xanl 2016-05-19
  • 打赏
  • 举报
回复
你可以参考一下

 angular.module('services',[])
.factory('authSvc',function($http){
	return {
		login:function(formData,callback){
			$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
			/*$http.post('http://localhost:53595/home/test',JSON.stringify(formData))
			.success(function(responseData){
				callback(responseData);
			});*/
			$http.jsonp('http://localhost:53595/home/test',JSON.stringify(formData))
			.success(function(responseData){
				callback(responseData);
			});
		},
		httptest:function(formData,callback){
			$http({
				method:'post',
				url:'http://localhost:53595/home/test',
				params:formData,
				headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
			})
			.success(function(responseData){
				callback(responseData);
			})
			.error(function(){
				alert('server error');
			})
		}
	};
})
最近在学ionic 、几天前写的,之前也是接不到数据
baidu_35028162 2016-05-19
  • 打赏
  • 举报
回复
引用 4 楼 u010811342 的回复:
正常来说要用回调函数处理数据变化,整个方法都是异步的,不可能通过return 获取返回值

factory.getjson = function (callbackFunc) {
            return $http({ cache: true, url: baseurl + "/Home/getjson", method: 'GET' })
                        .success(function(datat){
                        alert(JSON.stringify(data));
                        callbackFunc(data);
             })
        }
我想返回一个数组给$scope.rowCollection ,js里面完整应该怎么写
Xanl 2016-05-19
  • 打赏
  • 举报
回复
正常来说要用回调函数处理数据变化,整个方法都是异步的,不可能通过return 获取返回值

factory.getjson = function (callbackFunc) {
            return $http({ cache: true, url: baseurl + "/Home/getjson", method: 'GET' })
                        .success(function(datat){
                        alert(JSON.stringify(data));
                        callbackFunc(data);
             })
        }
Xanl 2016-05-19
  • 打赏
  • 举报
回复
异步方法、你想得到什么返回值?

factory.getjson = function () {
            return $http({ cache: true, url: baseurl + "/Home/getjson", method: 'GET' })
                        .success(function(datat){
                        alert(JSON.stringify(data));
             })
        }
baidu_35028162 2016-05-18
  • 打赏
  • 举报
回复
我觉得应该是js代码错了 ,应该怎么改

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧