我试图从javascript调用c#方法。我有一个asp.net页面是parameteres.aspx,但按下按钮会显示错误404.
这是parameters.aspx.cs中的方法
[WebMethod]
public void MethodSearch()
{
//Search
string _sEnrollmentEsiid;
string _sEnrollmentAddress;
string _sEnrollmentCity;
string _sEnrollmentZipCode;
//string _sAMS;
//_sEsiidText
GetDistributionPointsRequest disRequest = new GetDistributionPointsRequest();
_sEnrollmentEsiid = disRequest.EsiID;
_sEnrollmentAddress = disRequest.Address;
_sEnrollmentCity = disRequest.City;
_sEnrollmentZipCode = disRequest.Zip;
}
这是javascript中的函数:
<script type="text/javascript">
function testFunction() {
$.ajax({
type: "POST",
url: 'http://localhost:63788/parameters.aspx/MethodSearch',
//dataType: "json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#divResult").html("success");
},
error: function (e) {
$("#divResult").html("Something Wrong.");
}
});
}
</script>
以及我如何在HTML中调用该函数
<a href class="btn btn-danger" onclick="testFunction()">Test</a>
<label id="divResult"></label>
有人可以告诉我,我做错了什么吗? 错误是:
无法加载资源:服务器响应状态为500(内部服务器错误)
答案 0 :(得分:0)
要在您的网页上调用[WebMethod]
,该方法必须标记为static
。当然你可能也希望它有一个返回值(假设它是一个搜索函数),但我相信缺少static
是你的具体问题。如果您的代码存在其他问题,我们可以在您的方法被调用后发现它们。
[WebMethod]
public static void MethodSearch()
{
//Search
string _sEnrollmentEsiid;
string _sEnrollmentAddress;
string _sEnrollmentCity;
string _sEnrollmentZipCode;
//string _sAMS;
//_sEsiidText
GetDistributionPointsRequest disRequest = new GetDistributionPointsRequest();
_sEnrollmentEsiid = disRequest.EsiID;
_sEnrollmentAddress = disRequest.Address;
_sEnrollmentCity = disRequest.City;
_sEnrollmentZipCode = disRequest.Zip;
}