gpt4 book ai didi

javascript - Angular 响应在末尾附加对象

转载 作者:行者123 更新时间:2023-11-27 23:15:41 25 4
gpt4 key购买 nike

这是我的 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));
}

响应对象是这样的 - 在 {"d":null} 末尾附加了一些奇怪的行,我不明白为什么。我还在客户端收到错误: SyntaxError: Unexpected token:

"[{"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}"

最佳答案

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

我已经找到解决办法了。我已将服务器端函数末尾的代码更改为:

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

现在响应正常。

关于javascript - Angular 响应在末尾附加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35852548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com