我如何解析来自asmx的http响应对象

时间:2017-09-11 05:14:34

标签: asp.net angularjs asmx



$scope.Edit = function (id) {
console.log("edit id : " + id);
$scope.Employee = {};
$scope.eid = id;
var data = JSON.stringify({empid: $scope.eid});
var url = "/services/EmployeeService.asmx/EditEmployee";
$http.post(url, data).then(function (response) {
    $scope.Employee = response.data;
    console.log($scope.Employee.fname);
    console.log($scope.Employee);
    var mydata = jQuery.parseJSON(JSON.stringify(response.data));
    console.log(mydata);
}, function (response) {
    console.log(response.status);
    console.log(response.statusText);
});

}




[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string EditEmployee(int empid)
{
    Employee employee = new Employee();
    if (emplist.Count > 0)
    {
        foreach (Employee emp in emplist)
        {
            if (emp.empId == empid)
            {
                employee.empId = empid;
                employee.fname = emp.fname;
                employee.city = emp.city;
                employee.mobile = emp.mobile;
                employee.country = emp.country;
                break;
            }
        }
    }
    JavaScriptSerializer js = new JavaScriptSerializer();
    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    List<Employee> elist = new List<Employee>();
    elist.Add(employee);
    return new JavaScriptSerializer().Serialize(elist);
}

这是我从回复中得到的

  

对象   d:&#34; [{&#34; EMPID&#34;:103,&#34; FNAME&#34;:&#34; sujith&#34;&#34;城市&#34;:&#34;特利奇&#34;&#34;移动&#34;:&#34; 56456456&#34;&#34;国家&#34;:&#34;印度&#34;}]&#34;    proto :对象

如何解析角度js对象。我想这样访问:$scop.Employee.empId,$scope.Employee.fname

谢谢&amp;问候 阿伦

4 个答案:

答案 0 :(得分:1)

您确定您收到的对象是以&#34;开头和结尾吗? &#34; (双引号)

答案 1 :(得分:0)

更改以下行

$scope.Employee = response.data;

$scope.Employee = response.data.d[0];

答案 2 :(得分:0)

试试这个。这应该有效:

 $scope.Edit = function (id) { console.log("edit id : " + id); $scope.Employee = {}; $scope.eid = id; var data = JSON.stringify({empid: $scope.eid}); var url = "/services/EmployeeService.asmx/EditEmployee"; $http.post(url, data).then(function (response) {
    $scope.Employee = response.data.d[0];
    console.log($scope.Employee.fname);
    console.log($scope.Employee); }, function (response) {
    console.log(response.status);
    console.log(response.statusText); });

}

答案 3 :(得分:0)

 $scope.Edit = function (id) {
                        console.log("edit id : " + id);
                        $scope.Employee = {};
                        $scope.eid = id;
                        var data = JSON.stringify({ empid: $scope.eid });
                        var url = "/services/EmployeeService.asmx/EditEmployee";
                        $http.post(url, data).then(function (response) {
                            $scope.Employee = JSON.parse( response.data.d);
                            console.log("empid: " + $scope.Employee.empId);
                            console.log("fname: " + $scope.Employee.fname);
                            console.log("city: " + $scope.Employee.city);
                            console.log("country: " + $scope.Employee.country);                         
                                                   
                        }, function (response) {
                            console.log(response.status);
                            console.log(response.statusText);
                        });
                            
                    }

enter code here
[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string EditEmployee(int empid)
        {
            Employee employee = new Employee();
            if (emplist.Count > 0)
            {
                foreach (Employee emp in emplist)
                {
                    if (emp.empId == empid)
                    {
                        employee.empId = empid;
                        employee.fname = emp.fname;
                        employee.city = emp.city;
                        employee.mobile = emp.mobile;
                        employee.country = emp.country;
                        break;
                    }
                }
            }           
            return new JavaScriptSerializer().Serialize(employee);
        }
enter code here

谢谢Vikas Thakur,Abhijeet Jaiswal,它帮助我解决了它,现在它正在工作,asmx webservice webmethod有什么错误我应该将Employee对象作为字符串返回,之前我错误地将它添加到列表并返回列表。 webmethod返回类型是字符串,因此它不解析。现在它正在解析。谢谢你们两个帮帮我。