我想使用jquery ajax get方法调用web方法。但是它没有被调用。低于我的javascript和代码
的javascript:
function RetrievePassword() {
var forgotEmail = $("#txtForgotEmail").val().trim();
//call the ajax method to retrieve the password from the email address provided.
$.ajax({
type: "GET",
url: "test.aspx/RetrievePassword?email=" + forgotEmail,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result.d);
},
error: function () {
alert("Error while calling the server!");
}
});
}
我的代码隐藏功能
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public static string RetrievePassword(string email)
{
//some code
}
任何人都可以帮我这个..
答案 0 :(得分:1)
出于安全考虑,ASP.Net AJAX
页面方法仅支持POST
次请求。
以下示例使用POST
请求
的jQuery
$.ajax({
type: "POST",
url: "test.aspx/RetrievePassword",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{email:"' + forgotEmail + '"}',
success: function (result) {
alert(result.d);
},
error: function () {
alert("Error while calling the server!");
}
});
C#Pagemethod
[WebMethod]
public static void RetrievePassword(string email)
{
//some code
}
请记住使用pagemethod参数中使用的ajax post数据变量名。因为它具有案例意义
答案 1 :(得分:0)
在您的代码中尝试此操作:
$.ajax({
url: 'URL',
data: "{ 'Keyword': '" + forgotEmail + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
console.log(item);
return {
item;
}
}))
},
error: function (response) {
console.log(response.responseText);
},
failure: function (response) {
console.log(response.responseText);
}
});
要确保您的代码,请始终使用console.log();
答案 2 :(得分:0)
在web.config
中尝试:
<system.web>
...
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
ScriptModule用于管理ASP.NET中用于AJAX功能的HTTP模块,有时asp.net会不正确地加载它。您必须在web.config
中手动添加ScriptModule。
参考: