我确信我在这里遗漏了一些明显的东西。我在SO上阅读了各种帖子:
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ using jQuery to call webMethod returns HTML... webMethod not called at all
但是看不出我有什么不对。任何调用webmethod的尝试都会导致返回完整的HTML页面。我使用的是.NET 4.51 WebForms。我的剧本是:
<script>
$(function() {
alert('here');
//Get the list of files already uploaded for this user
$.ajax({
type: "POST",
url: "/Maintenance/DesignerImages2/UserExistingImagesGet",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert('success');
var data2 = data.hasOwnProperty("d") ? data.d : data;
alert(data2);
},
error: function (data) {
alert('error');
var data2 = data.hasOwnProperty("d") ? data.d : data;
alert(data2);
}
});
alert('here2');
});
</script>
和/ Maintenance / DesignerImages2背后的代码是:
[WebMethod]
public static string UserExistingImagesGet()
{
return DateTime.Now.ToString();
}
我在Web.config中有以下内容:
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions" />
</httpModules>
答案 0 :(得分:0)
尝试将url
值更改为“/Maintenance/DesignerImages2.aspx/UserExistingImagesGet”。