我正在尝试为我的电子商务网站添加上传表单,我需要为类别和子类别选择框。当我选择一个类别时,我想要一个ajax函数来自动填充其他选择框选项,其中包含子类别的sql server表中的子类别列表。我还没有能够使ajax函数的url部分工作,也没能找到与我的c#class和cshtml页面一起使用的正确url。谢谢。 [这是测试ajax函数的图片,想在我的测试cshtml页面中用于测试,名为list] (https://i.stack.imgur.com/988Rg.png)
@{
Layout = "~/_SiteLayout3.cshtml";
}
<script>
$(document).ready(function () {
$.ajax({
type: "POST",
url: '<%= ResolveUrl("~/Shared/MySite1.cs/GetData2") %>',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#Content").text(response.d);
},
failure: function (response) {
alert(response.d);
}
});
});
</script>
<form id="frm" method="post">
<div id="Content">
</div>
</form>
My C# Code
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Web.Services;
public static class MySite1
{
[WebMethod]
public static string GetData2()
{
return "This string is from Code behind";
}
}
答案 0 :(得分:0)
对我来说已经有一段时间了,所以如果记忆服务,在ASP.Net Web Pages中,它更像是“裸机”。您必须自己处理HTTP请求。
这个琐碎的示例使用.cshtml
,但由于这仍然是“ASP.Net”,您也可以使用通用处理程序(.ashx
)(因为有不需要“内容”,只有一些结果由您的XHR请求处理)。
<p>Ajax result appears below:</p>
<p id="result"></p>
<script>
$(function () {
$("#ajaxTest").on("click", function () {
var request = $.ajax({
type: "POST",
url: "/ajaxhandler",
data: { "foo": "1234bar" }
});
request.done(function (d) {
console.log(d);
$("#result").text(d);
});
request.fail(function (xhr, err) {
console.log("It died...%o", err);
});
});
})
</script>
上面的/ajaxhandler
是.cshmtl
文件(如上所示,您可以使用.ashx
代替):
ajaxhandler.cshtml
:
@{
if (IsPost)
{
var payload = "";
using (var reader = new StreamReader(Request.InputStream))
{
payload = reader.ReadToEnd();
}
Response.AddHeader("Content-Type", "text/text");
Response.Write(payload);
}
}
您可以通过检查HTTP请求(例如url,片段,查询字符串等)来进行扩展,并相应地进行处理。
H个。