How To Return Web API Data

This question already has an answer here:

  • How do I get ASP.NET Web API to return JSON instead of XML using Chrome? 29 answers

  • C#

    [WebMethod]
    public string GetEmployeeData(int userId)
    {
        Dictionary<string, object> returnData = Dictionary<string, object>();
    
        using(SqlConnection conn = new SqlConnection(connString))
        {
            //etc... etc...
        }
    
        var jsonSerializer = new JavaScriptSerializer();
    
        return jsonSerializer.Serialize(returnData);
    }
    

    Javascript:

    $http.post('MyApp/GetEmployeeData', { userId: 1 }).then(function(response){
        $scope.myData = response.data;
    }).catch(function(response) {
        //an error has occurred
    });
    
    链接地址: http://www.djcxy.com/p/20418.html

    上一篇: 将对象转换为HttpContent

    下一篇: 如何返回Web API数据