简体   繁体   English

从ASP.NET中的操作方法检索Json对象

[英]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.

相关问题 如何从ASP.NET MVC操作方法返回JSON对象 - How to Return a JSON Object from an ASP.NET MVC Action Method 对象不支持IE中的属性或方法,因此会从javascript,asp.net调用Action脚本函数 - Object doesn't support property or method in IE, calling Action script function from javascript, asp.net 将数据从javascript传递到asp.net MVC中的操作方法 - Passing data from javascript to action method in asp.net MVC ASP.NET WebMethod返回对象为json但不在响应方法中 - ASP.NET WebMethod returning object as json but not in response method javascript-如何将JSON对象中的数组从JavaScript传递给asp.net MVC控制器方法? - How to pass array in a JSON object from javascript to asp.net mvc controller method? 从同一域中检索Json数据,如何使用JsonP通过ASP.NET从另一个域中获取数据? - Retrieving Json data from same domain, how to use JsonP to get from another domain with asp.NET? asp.net mvc将Json对象从视图传递到控制器 - asp.net mvc pass Json object from view to controller 可以从ASP.NET控制器查看JSON对象 - JSON object from ASP.NET controller to view asp.net mvc操作方法将数据发布为json数据除外 - asp.net mvc action method post data except as json data 如何将嵌套的 JavaScript 对象传递给 ASP.NET MVC 操作方法? - How to pass a nested JavaScript object to ASP.NET MVC Action Method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM