开发者

asp.net 4 jQuery Ajax not working with page method in iis

开发者 https://www.devze.com 2023-04-05 02:42 出处:网络
Im normally an MVC chap, so jquery ajax is usually a breeze. Im working on an old webforms project though and cannot get jquery ajax to talk to a webmethod on my local iis. It works on visual studios

Im normally an MVC chap, so jquery ajax is usually a breeze. Im working on an old webforms project though and cannot get jquery ajax to talk to a webmethod on my local iis. It works on visual studios own web server though.

The jquery is:

//Collect data
    var prefs开发者_如何学JAVA = {
        'pref_1': $('input#pref_1').val(),
        'pref_2': $('input#pref_2').val(),
        'pref_3': $('input#pref_3').val(),
        'pref_4': $('input#pref_4').val()
    }

    //Send data via ajax
    //Uncomment below to send requests
    var loc = window.location.href;
    var substr = loc.split("?");
    $.ajax({
        url:  substr[0] + "/SetUserPreferences",
        type: "POST",
        data: JSON.stringify(prefs),
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    });

As you can see I'm taking the location href and searching for the method in there.

In the index.aspx page I have:

[WebMethod]
public static void SetUserPreferences(string pref_1, string pref_2, string pref_3, string pref_4)
{
    using (lbDataContext dc = new lbDataContext())
    {
        MoreBang_Preference preference = new MoreBang_Preference();
        preference.UserId = (int)HttpContext.Current.Session["UserId"];
        preference.DateSubmitted = DateTime.Now;
        preference.Preference1 = pref_1;
        preference.Preference2 = pref_2;
        preference.Preference3 = pref_3;
        preference.Preference4 = pref_4;
        dc.MoreBang_Preferences.InsertOnSubmit(preference);
        dc.SubmitChanges();
    }
}

The site I am working on is an umbraco site, it has

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

in the httpModules section of the web config and is running in Asp.Net 4 integrated mode.

It also has

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

in the modules tag of the system.webServer section of the web config.

I am genuinly stumped as to why it isnt working.

Many thanks in advance to those who contribute!


In the end I changed to using a webservice instead of a page method and posted the session value to a hidden field on the page which was then sent to the web service. Not as clean as id have liked but working now. Frustrating as i have never had problems getting this to work before on iis.

0

精彩评论

暂无评论...
验证码 换一张
取 消