C# AJAX请求不能与路由C一起工作#
我已在ajax请求中包括以下内容:C# AJAX请求不能与路由C一起工作#,c#,ajax,routing,C#,Ajax,Routing,我已在ajax请求中包括以下内容: $(function () { $("#DropDownList1").change(function () { alert($("#DropDownList1")[0].value); $.ajax({ type: "POST", url: '<%= Page.ResolveUrl("~/bikesearch.aspx/GetModels") %>', data: '{id: "'
$(function () {
$("#DropDownList1").change(function () {
alert($("#DropDownList1")[0].value);
$.ajax({
type: "POST",
url: '<%= Page.ResolveUrl("~/bikesearch.aspx/GetModels") %>',
data: '{id: "' + $("#DropDownList1")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (r) {
var ddlCustomers = $("[id*=DropDownList2]");
ddlCustomers.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(r.d, function () {
ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
});
});
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new ListCompactionConverter());
settings.Formatting = Formatting.Indented;
var result = JsonConvert.SerializeObject(customers,settings);
return(result);
下面是my WebMethod返回的内容:
[System.Web.Services.WebMethod]
public static List<ListItem> GetModels(int id)
{
connection conn = new connection();
string query = "SELECT Id, Model FROM Model where Manufacturer_Id = " + id;
using (SqlConnection con = new SqlConnection(conn.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand(query))
{
List<ListItem> customers = new List<ListItem>();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new ListItem
{
Value = sdr["Id"].ToString(),
Text = sdr["Model"].ToString()
});
}
}
con.Close();
return customers;
}
}
}
啊哈,你的最后一条评论对我很有帮助,如果你想将对象传递给Ajax请求,你必须将对象序列化为
Json
:
$(function () {
$("#DropDownList1").change(function () {
alert($("#DropDownList1")[0].value);
$.ajax({
type: "POST",
url: '<%= Page.ResolveUrl("~/bikesearch.aspx/GetModels") %>',
data: '{id: "' + $("#DropDownList1")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (r) {
var ddlCustomers = $("[id*=DropDownList2]");
ddlCustomers.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(r.d, function () {
ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
});
});
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new ListCompactionConverter());
settings.Formatting = Formatting.Indented;
var result = JsonConvert.SerializeObject(customers,settings);
return(result);
啊哈,你的最后一条评论对我很有帮助,如果你想将对象传递给Ajax请求,你必须将对象序列化为
Json
:
$(function () {
$("#DropDownList1").change(function () {
alert($("#DropDownList1")[0].value);
$.ajax({
type: "POST",
url: '<%= Page.ResolveUrl("~/bikesearch.aspx/GetModels") %>',
data: '{id: "' + $("#DropDownList1")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (r) {
var ddlCustomers = $("[id*=DropDownList2]");
ddlCustomers.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(r.d, function () {
ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
});
});
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new ListCompactionConverter());
settings.Formatting = Formatting.Indented;
var result = JsonConvert.SerializeObject(customers,settings);
return(result);
请在google chrome中打开bikesearch.aspx,当开发工具的Ajax请求捕获网络选项卡(网络选项卡中的过滤器xhr)我以前从未使用过它时,我收到一条状态为400的错误请求消息。返回字符串的webmethod工作正常,但返回列表的webmethod失败,这是为什么?请在google chrome中打开bikesearch.aspx,当开发工具的Ajax请求捕获网络选项卡(网络选项卡中的过滤器xhr)我以前从未使用过它时,我收到一条状态为400的错误请求消息。返回字符串的webmethod工作正常,但返回列表的webmethod失败,这是为什么?谢谢Vahid,它工作正常。我现在唯一遇到问题的是返回(JsonConvert.SerializeObject(customers))只返回文本而不返回值项,例如[“RS125”、“RS50”…]和[{0,“RS125”},{1,“RS50”},…]。要显示正确的结果,需要对WebMethod和Ajax脚本进行哪些更改?在循环遍历结果时,Ajax总是返回未定义的结果。我使用以下链接清理json结果,我希望这样做不需要导入Newtonsoft.json包。我使用System.Web.Script.Serialization;JavaScriptSerializer();随着这一联系,一切似乎都很完美。谢谢你给我指出了正确的方向。谢谢瓦希德,成功了。我现在唯一遇到问题的是返回(JsonConvert.SerializeObject(customers))只返回文本而不返回值项,例如[“RS125”、“RS50”…]和[{0,“RS125”},{1,“RS50”},…]。要显示正确的结果,需要对WebMethod和Ajax脚本进行哪些更改?在循环遍历结果时,Ajax总是返回未定义的结果。我使用以下链接清理json结果,我希望这样做不需要导入Newtonsoft.json包。我使用System.Web.Script.Serialization;JavaScriptSerializer();随着这一联系,一切似乎都很完美。谢谢你给我指明了正确的方向。