ASP.NET 4 jQuery的AJAX的WebMethod调用

问题描述:

在ASP.NET 3.5我有这个JavaScript的网页(Default.aspx的)上:

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);
        }
    });
}

通过这背后的code(default.aspx.cs):

With this in the code behind (default.aspx.cs):

[System.Web.Services.WebMethod]
public static string LoadNewsItems() {
    return "test1";
}

我有一个ScriptManager用的EnablePageMethods =真正的在页面上。所有工作的罚款。

I have a ScriptManager on the page with EnablePageMethods=true. All worked fine.

现在该项目升级到ASP.NET 4.0,并使用新的URL路由功能。 Ajax调用不再工作了。在Firebug我看到它返回,而不是XML响应的完整的页面。

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.

什么在ASP.NET 4改变了这可能是导致此错误?

What has changed in ASP.NET 4 that could be causing this error?

固定,

修改

url: "default.aspx/LoadNewsItems",

url: '<%= ResolveUrl("default.aspx/LoadNewsItems") %>',

它做的URL路径。

It has to do with the URL Routing.