NoBullMan
NoBullMan

Reputation: 2184

webmethod is not fired

My first attempt to get a server side function called from javascript is not working as the webmethod is not getting called.

The aspx file contains a bootstrap styled button; when clicked, I need to add a record to user's "Favorites" list (add a record in database).

The page inherits from a master page and master page contains:

<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" />

aspx page has:

<script type="text/javascript">
function addFavorite(url, friendly) {debugger
    PageMethods.AddFavorite(url, friendly, onSuccess);
}
function onSuccess(result, userContext, methodName) {debugger
    alert(result);
}
</script>

<button type="button" class="btn btn-primary btn-xs" onclick="addFavorite('some_url', 'some firendly name');">
<i class="fa fa-heart-o" aria-hidden="true"></i>Favorites</button>

In code behind:

[WebMethod]
public static string AddFavorite(string sURL, string sFriendlyName)
{
    // This is where I would add a record to a DB table, but for testing ...
    return sFriendlyName;
}

When I click the button, it hits addFavorites() and immediately onSuccess and bypasses web method, it hen displays the page source on an alert box (partially of course).

I have searched and can't seem to know what I am doing wrong. All examples show the same steps I have taken.

Upvotes: 0

Views: 1291

Answers (2)

ashin
ashin

Reputation: 2603

I could repro this on my local machine. Noticed that the script module was not registered in the modules section in the web.config!

Make sure you include the script module as follows in your web.config to make it work:

<system.webServer>
          <modules>
           <!-- *...other registered modules..* -->
           <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
           </modules>
    </system.webServer>

Update

Make sure you have FriendlyUrlSettings.AutoRedirectMode set to Off, otherwise the page method request would return 401 UnAuthorized. The code should be set as follows in the RouteConfig:

public static class RouteConfig  
{  
    public static void RegisterRoutes(RouteCollection routes)  
    {  
        var settings = new FriendlyUrlSettings();  
        settings.AutoRedirectMode = RedirectMode.Off;  
        routes.EnableFriendlyUrls(settings);  
    }  
}  

In case you want to retain the Friendly URLs, then add your own friendly url resolver that inherits from WebFormsFriendlyUrlResolver as follows:

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings, new WebMethodFriendlyUrlResolver());
    }
}

public class WebMethodFriendlyUrlResolver : WebFormsFriendlyUrlResolver
{
    public override string ConvertToFriendlyUrl(string path)
    {
        if (HttpContext.Current.Request.PathInfo != string.Empty)
        {
            return path;
        }
        else
        {
            return base.ConvertToFriendlyUrl(path);
        }
    }
}

If friendly URL is enabled, JavaScript callback function must be updated to set the path to include the .aspx extension explicitly:

function addFavorite(url, friendly) {  
        PageMethods.set_path(PageMethods.get_path() + '.aspx');  
        PageMethods.AddFavorite(url, friendly, onSuccess, onError);  
}  

Upvotes: 5

wazz
wazz

Reputation: 5058

add an onFailure method to see if it gets hit. (it might be required, but i'm not positive.) edit: i don't think it's required. i just copied your code and it works here.

have to ask, just to be sure: those js functions are inside script tags? (i guess so, if 'success' is getting hit. just checking.)

one more check: code behind using statement required: using System.Web.Services;.

Upvotes: 0

Related Questions