Call ASP.NET PageMethod/WebMethod with jQuery - returns whole page

不打扰是莪最后的温柔 提交于 2019-11-26 18:37:32

Do you know that Page Methods are working properly? If you use the the ScriptManager do they work?

It sounds like you might be missing a web.config entry. Specifically the HttpModules section.

I was missing one line from my web.config:

<system.web>
  <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </httpModules>
</system.web>

I encountered this problem again today for a different reason: I had misspelled "application" in

contentType: 'application/json'

And was getting a full-page response instead of a call to the WebMethod.

If you have tried all that and still get the whole page returned from your pagemethod, you may want to make sure you are not using friendly urls. If you are using them, this trick may help you

Add this line on your js script before you make the call:

PageMethods.set_path(PageMethods.get_path() + '.aspx');

Throwing this out here as a side note. I was getting that error due to the length of my string variables in my HTML string and the website I used to get my ajax called looked like this.

loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Default.aspx" : loc;
        $.ajax({
            type: "POST",
            url: loc + "/" + methodName,
            data: "{" + args + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: onSuccess,
            error: onFail
        });

It wasn't capable of extracting the .aspx link correctly, so I just hardcoded my webpage instead of using the loc var.

Most ajax scenarios I've seen really should call a web service or separate script handler, not a page. That is extremely easy to do in .net 3-5, not so easy in 2-0. Even after you figure out (if) how not to load the whole page, here are reasons not to call a page method:

1) The page method might load less stuff than a full page load, but still far more than you need for a simple ajax call. 2) Lousy separation of responsibilities. The page is probably responsible for nicely laying stuff out, not the logic you are using in the ajax method.
3) Maybe you need some session state, but that should still be available.

I'm currently updating my knowledge on this subject ... I'll look for a good answer to this question in this thread, or I'll post one next week. Here is the direction I am headed

1) Send JSON from server to client, and use javascript to update your page. - a variety of frameworks make it easy to produce JSON from the web server.
2) JQuery makes ajax calls, json handling, and client formatting fun, instead of painful.

After almost two hours and after had tried everything i finally solved it.@Marvin Zumbado's comment helped me.I was missing the .aspx from my url.I know this is not my best moment as a programmer!

Commenting the following line in RouteConfig.cs works for me

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