问题
In ASP.NET 3.5 I had this javascript on a page (default.aspx):
function getMoreNewsItems() {
$.ajax({
type: "POST",
url: "default.aspx/LoadNewsItems",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
}
With this in the code behind (default.aspx.cs):
[System.Web.Services.WebMethod]
public static string LoadNewsItems() {
return "test1";
}
I have a ScriptManager on the page with EnablePageMethods=true. All worked fine.
Now the project upgraded to ASP.NET 4.0 and is using the new url routing functionality. The AJAX call doesn't work anymore. In FireBug I see it returns the complete page, instead of the XML response.
What has changed in ASP.NET 4 that could be causing this error?
回答1:
Fixed,
Change
url: "default.aspx/LoadNewsItems",
To
url: '<%= ResolveUrl("default.aspx/LoadNewsItems") %>',
It has to do with the URL Routing.
来源:https://stackoverflow.com/questions/4119067/asp-net-4-jquery-ajax-webmethod-call