这是我的 Default.aspx + jQuery 脚本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="Sample001.Default" %>
<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="masterlbl" Text="Master" runat="server" />
</td>
<td>
<span class="Mastercs">
<asp:DropDownList ID="ddl1" runat="server">
<asp:ListItem Text="Item1" Value="Item1" />
<asp:ListItem Text="Item2" Value="Item2" />
<asp:ListItem Text="Item3" Value="Item3" />
<asp:ListItem Text="Item4" Value="Item4" />
<asp:ListItem Text="Item5" Value="Item5" />
</asp:DropDownList>
</span>
</td>
<td>
<asp:Label ID="slavelbl" Text="Slave" runat="server" />
</td>
<td>
<span class="slavecs">
<asp:DropDownList ID="ddl2" runat="server" />
</span>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$('span.Mastercs select').change(function () {
$.ajax({
type: "POST",
url: 'MyHandler.ashx',
dataType: "text",
data: "ItemSelected=" + $('select#ddl1').val(),
async: true,
success: function (data) { Handler_Success(data); }
});
});
function Handler_Success(data) {
$('select#ddl2').empty();
$.each(data, function (i, slaveValue) {
$('select#ddl2').append($('<option></option>')
.val(data.Value).html(data.Text));
});
}
});
</script>
</body>
</html>
上面的代码工作正常。 但我还需要传递一个参数,这就出现了问题。 我尝试This Way还有以下代码,但检索空值:
$.ajax({
type: "POST",
url: 'MyHandler.ashx',
dataType: "text",
data: '{"ItemSelected=" ' + $('select#ddl1').val() + ', "ID="' +
$('select#ddl1').attr('id') + ' }',
async: true,
success: function (data) { Handler_Success(data); }
});
});
问题出在哪里?
编辑:
我还需要使用 HttpHandler 而不是 WebService 或 WebMethod
这是我的 HttpHandler:
public class SlaveValue {
public string Value { get; set; }
public string Text { get; set; }
}
public class SlaveValueHandler : IHttpHandler {
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
string valueSelected = context.Request["ItemSelected"];
string id = context.Request["ID"];
List<SlaveValue> slaveValues = new List<SlaveValue>();
SlaveValue sv;
sv = new SlaveValue();
sv.Text = "SV1";
sv.Value = valueSelected + id + "s1";
slaveValues.Add(sv);
string responseText = Newtonsoft.Json.JsonConvert.SerializeObject(slaveValues);
context.Response.ContentType = "text/json";
context.Response.Write(responseText);
}
}
还有我的 WebConfig:
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="Slave Handler" verb="*" path="MyHandler.ashx"
type="Sample001.SlaveValueHandler,Sample001" preCondition="integratedMode" />
</handlers>
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
编辑:
我在这里找到它: CodeProject
最佳答案
如果要以文本格式传递数据,则可能只能有一个参数 - 毕竟,.NET 如何知道第一个参数在哪里结束,第二个参数在哪里开始?
相反,请尝试以 JSON 格式传递数据,并将数据参数设置为使用 JSON 格式。
$.ajax({
type: "POST",
url: 'MyHandler.ashx',
dataType: "application/json",
data: '{"ItemSelected":"' + $('select#ddl1').val() + '", "ID":"' +
$('select#ddl1').attr('id') + '" }',
async: true,
success: function (data) { Handler_Success(data); }
});
});
不过,这可能不适用于像您这样的 HTTP 处理程序。通常您希望在 Web 服务 (.asmx) 文件中执行此操作,并定义一个 webmethod 来完成该工作。
请参阅 Dave Ward 的网站,http://www.encosia.com , 更多细节。 This post是一个合理的总结。
关于jquery - 在 jQuery Ajax 脚本中传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7977533/