2017-09-11 80 views
0

我怎麼解析HTTP響應對象從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」}]「 proto:Object

我該如何解析角度js對象。我要像這樣訪問:$scop.Employee.empId,$scope.Employee.fname

感謝&問候 阿倫

回答

0

更改以下行

$scope.Employee = response.data; 

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

嗨Vikas Thakur,感謝您的幫助,但它仍然返回undefined, –

+0

$ scope.Employee = response.data.d [0]; console.log($ scope.Employee.fname); // return undefined –

+1

console.log(response.data.d); //返回[{「empId」:103,「fname」:「sujith」,「city」:「trichy」,「mobile」:「56456456」,「country」:「India」}] console.log(response .data.d [0]); //返回[ –

1

你確定你收到的對象以「」開頭和結尾(雙引號)

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); }); 

} 
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 

謝謝維卡斯·塔庫爾,Abhijeet Jaiswal,它幫我解決了這個問題,現在是工作,是在ASMX Web服務的WebMethod什麼錯誤我應該返回Employee對象作爲字符串,以前我錯誤地將它添加到列表並返回列表。 webmethod返回類型是字符串,所以它不解析。現在它正在解析。謝謝你們兩位幫助我。

+0

標記接受的答案之一。這將有助於處於類似情況的其他人 –

+0

我已經標記接受了您的回答,它表示聲望超過15可以接受 –