[英]Retrieving Json Object from action method in ASP.NET
I have searched and seen many solutions but yet I still am not getting any progress. 我已经搜索并看到了许多解决方案,但是仍然没有任何进展。 I get no errors within the console and get nothing in return from the ajax request. 我在控制台内没有收到任何错误,也没有从ajax请求中得到任何回报。
Here is action method code: 这是动作方法代码:
[HttpGet]
public ActionResult GetEmployees()
{
AWEmployeeModel aw = new AWEmployeeModel();
aw.ID = 0;
aw.Name = "Selena";
aw.Position = "Cashier";
aw.Department = "Finance";
return Json(aw,JsonRequestBehavior.AllowGet);
}
And here is my ajax call within my controller: 这是我在控制器内的ajax调用:
EmpApp.controller("AWEmpControl", function ($scope) {
loadEmployees();
function loadEmployees() {
$.ajax({
url: "/AWEmployee/Index/GetEmployees",
method: "GET",
success:function (response) {
$scope.employees = response["Name"];
}
});
}
});
I do also have the ng directives on the desired page: 在所需页面上也有ng指令:
<div class="container" ng-app="AWEmpApp" ng-controller="AWEmpControl">
{{employees}}
</div>
I tested to see if the scripts work fine by assigning $scope.employees = "Hello..."; 我通过分配$ scope.employees =“ Hello ...”;测试脚本是否可以正常工作; and no problems, so I know it has nothing to do with script loading.... What could be the issue? 没问题,所以我知道这与脚本加载无关。...可能是什么问题? I've made sure to describe the request as GET and to return a JSON object. 我已经确保将请求描述为GET并返回一个JSON对象。 Am I missing something? 我想念什么吗?
Thanks in advance. 提前致谢。
EDIT 编辑
I checked the firefox console and got back Parsing Error: No XML found at location....... 我检查了firefox控制台并返回解析错误:在该位置找不到XML。
Which I found odd since the server returns a JSON object, so I replaced the ajax method with the following: 由于服务器返回JSON对象,我觉得很奇怪,所以我用以下代码替换了ajax方法:
function loadEmployees() {
$http.get('/AWEmployee/Index/GetEmployees')
.then(function(response) {
$scope.employees =response;
});
};
Now response is html data of the page that called it (Index), instead of the JSON object ! 现在响应是调用它的页面(索引)的html数据,而不是JSON对象! I assume something is happening in between the communication as suggested in this post . 我假设的东西是在建议的通信之间发生的事情在这个岗位 。
As you use JQuery to fetch data from server instead of $http
service and moreover success
function is executed asynchronous, AngularJS will not reflect any changes that was happened, to force it, just wrap assigning into $apply
: 当您使用JQuery从服务器而不是$http
服务获取数据并且success
执行函数是异步执行时,AngularJS不会反映发生的任何更改来强制执行,只需将分配包装到$apply
:
function loadEmployees() {
$.ajax({
url: "/AWEmployee/Index/GetEmployees",
method: "GET",
success:function (response) {
$scope.$apply(function(){
$scope.employees = response["Name"];
});
}
});
}
Found out the issue, it's a simple mistake on my part. 找出问题所在,这对我来说是一个简单的错误。 I was calling http://localhost:53729/AWEmployees/Index/GetEmployees which was the wrong thing to do. 我打电话给http:// localhost:53729 / AWEmployees / Index / GetEmployees ,这是错误的做法。 I took out the Index page, so now the url looks like http://localhost:53729/AWEmployees/GetEmployees and it worked perfectly! 我取出了“索引”页面,所以现在该URL看起来像http:// localhost:53729 / AWEmployees / GetEmployees ,它可以正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.