繁体   English   中英

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

[英]how do I parse the http response object coming from 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:“ [{” empId“:103,” fname“:” sujith“,” city“:” trichy“,” mobile“:” 56456456“,” country“:” India“}]”“ 原型 :对象

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

感谢和问候阿伦

您确定接收到的对象以“”(双引号)开头和结尾吗?

更改以下行

$scope.Employee = response.data;

$scope.Employee = response.data.d[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); });

}

  $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的返回类型为字符串,因此无法解析。 现在它正在解析。 谢谢你们俩帮助了我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM