角度响应在末尾添加对象

时间:2016-03-07 19:42:33

标签: javascript angularjs

这是我的POST请求:

$scope.TestPost = function (par1, par2) {

                $http.post('EmployeeService.asmx/GetAllEmployees',
                    {                    
                        par1: par1,
                        par2: par2       
                })
                .then(function (response) {
                    $scope.employees = response.data;
                })
            };

这是在服务器端调用的代码。正确调用代码并将json序列化对象写入响应:

[WebMethod]
public void GetAllEmployees(string par1, string par2)
{
    List<Employee> listEmployees = new List<Employee>();
    string cs = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
    using(SqlConnection con = new SqlConnection(cs))
    {
        List<Employee> _list = new List<Employee>();
        SqlCommand cmd = new SqlCommand("SELECT * FROM tblEmployees", con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        while(rdr.Read())
        {
            Employee emp = new Employee
            {
                id = Convert.ToInt32(rdr["Id"]),
                name = rdr["Name"].ToString(),
                gender = rdr["Gender"].ToString(),
                salary = Convert.ToInt32(rdr["Salary"])
            };
            listEmployees.Add(emp);
        }
    }

    JavaScriptSerializer js = new JavaScriptSerializer();            
    Context.Response.Write(js.Serialize(listEmployees));
}

响应对象是这样的 - 在末尾添加了一些奇怪的行{&#34; d&#34;:null}我无法理解为什么。我也在客户端收到错误:SyntaxError:意外的令牌:

"[{"id":1,"name":"Ben","gender":"Male","salary":55000},
{"id":2,"name":"Sara","gender":"Female","salary":68000},
{"id":3,"name":"Mark","gender":"Male","salary":57000},
{"id":4,"name":"Pam","gender":"Female","salary":53000},
{"id":5,"name":"Todd","gender":"Male","salary":60000}]{"d":null}"

1 个答案:

答案 0 :(得分:1)

感谢@ 82Tuskers和这篇文章: Differences between Response.End() and Response.Flush()

我找到了解决方案。我已将服务器端功能结束时的代码更改为:

Context.Response.Clear();
Context.Response.Write(js.Serialize(listEmployees));
Context.Response.Flush();
Context.Response.End(); 

现在回复正常。