ASP.NET 4 jquery ajax webmethod call

老子叫甜甜 提交于 2019-12-03 23:13:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!