c# - 如何使用 jQuery AJAX 调用 ASP.Net 字符串函数?
问题描述
我有一个字符串函数 ASP.Net Webform。我想使用 AJAX 调用这个函数。该函数从数据库中返回一个带有月份索引的字符串值
protected string BringDatas(int month)
{
Counts counts_ = new Counts();
return counts_.GetMonths(month);
}
var dataValue = { "month": 1 };
$.ajax({
type: "POST",
url: "Homepage.aspx/BringDatas",
data: dataValue,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
complete: function (jqXHR, status) {
alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
}
});
解决方案
这是javascript方面
<script type="text/javascript">
$(document).ready(
function () {
$("#Gonder").click(
function () {
$.ajax
({
type: "POST",
url: "Homepage.aspx/OrnekPost",
data: "{'parametre':'1234'}",
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (output) {
alert("Response: "+ output);
}, error: function () {
alert("hata var");
}
});
});
})
</script>
Codebehind.cs 代码
[ScriptMethod]
[WebMethod(EnableSession = true)]
public static string OrnekPost(string parametre)
{
return parametre + " değeriyle post işlemi gerçekleştirildi.";
}
推荐阅读
- python-3.x - 使用python获取sharepoint页面
- ios - 移动设备上的 Azure App Service 身份验证问题
- javascript - 使用 Postman 时,我们可以像变量一样在 for 循环中增加全局变量名称吗
- google-apps-script - 如何将多个文件上传保存在特定文件夹中?
- javascript - 设置输入字段的值后防止页面重新加载
- html - html gif 具有透明背景,但在 img 标签和 css 背景属性中变为白色
- .net - 如何从 Azure Web App 读取和写入文件到本地 NAS?
- c++ - 如何在 2D 中绘制/变形 QImage?
- python - 如何将 __str__ 方法应用于抽象超类的子类
- php - Mongodb 之类的带有两个字段的查询不起作用(使用 php)