Page load is fired instead of web method

好久不见. 提交于 2019-12-12 15:02:37

问题


OK, so my websites been working normally up until now, I'm not really sure what I've changed. I have a jQuery AJAX call that sends a coupon code to the server, and retrieves a number (which is the discount).

The Webmethod is no longer being fired though, instead the Page_load of the page which the webmethod is on is being fired. Why? What can I check? What can I do?

Here is my handler for when the button is clicked

$('div#code_apply_btn').click(function() {
    $(this).html('PLEASE WAIT');
    getpromocode();
});

Here is the AJAX call

function getpromocode(){
    var pcode = $('input#input_circuitcode').val();
    var hid = parseInt($('input#ss_id_h').val());
    $.ajax({
        type: "POST",
        url: "register.aspx/get_promocode",
        data: '{"promo":"' + pcode + '", "uid":' + hid + '}',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
            if (msg.d != -1) {
                applydiscount(msg.d);
                $('div#reg_circuit').show();
                $('div#circuit_promo').hide();
                $('div#reg_circuit').click();
            }
            else {
                $('input#input_circuitcode').val('');
                $('div#code_apply_btn').html('APPLY CODE');
            }
        },
        error: function (msg) {
            alert(msg);
        }
    });
}

Here's the webmethod

[WebMethod]
public static int get_promocode(string promo, int uid)
{
    return DAC.GetPromoCode(promo);
}

The webmethod is never called, but the Page_load event fires and runs through everything then it gets a 500 error because it shouldn't be called and doesn't have all the data it needs.

EDIT:

All my other pages that are using web methods work fine. It's just this page.

Another bit of strange behaviour: In chrome as soon as I begin to type "register.aspx", the Page_load is called. Again all my other pages are fine and this doesn't happen.


回答1:


I have found the answer to my question:

Because I have VS2008 I can only use .Net 3.5. My server however has .Net 4.0 or 2.0 for whatever reason I can't choose 3.5 as I would have liked to. So everytime I move my project I have to change the web.config because the default 3.5 config is filled with a whole bunch of stuff that 4.0 doesn't like.

So I remembered that I had emptied out the web.config to a barebone version that still worked in 3.5 which is where the problem is. I narrowed it down to these lines which I had excluded from the version running locally on 3.5

<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, 
        System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
        PublicKeyToken=31BF3856AD364E35"/>
</httpModules>

I guess the script module is what passes your jQuery requests to the web methods rather than the default page handler. There you go.. But no obvious errors or anything, it just didn't work.




回答2:


You must have EnablePageMethods="true" in ScriptManager on that page.



来源:https://stackoverflow.com/questions/9865557/page-load-is-fired-instead-of-web-method

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