0

I hope you can help me with this topic, I am working on a project with ASP.NET C #, the problem is that I need to click on a tag to execute a Json and this ends the session, for this you must call the method RestartSesion ().

All code is in the MasterPage.

The problem is that only the alert displays a text '' Error; ''.

I tell you what I did.

I have a VSesion class that has several methods, but the one I am working on today is RestartSesion.

public static void ReiniciarSesion()
{
     HttpContext.Current.Session.Abandon();        
     HttpContext.Current.Response.Redirect(Resources.SitePages.Login);
}

Template.Master.cs calls ReiniciarSesion.

[WebMethod]
public static void ReiniciarSession()
{
     VSesion.ReiniciarSesion();
}

Template.Master. Here is my trouble.

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $("#Reiniciar").click("click", function() {
            ReiniciarSession();
        });
    });
    function ReiniciarSession() {
        $.ajax({
            type: "POST",
            url: "Template.Master/ReiniciarSession",
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus + ": " + XMLHttpRequest.responseText);
            }
        });
    }
</script>

Here is the HTML template:

<ul>
    <li>
      <a href="#" id="Reiniciar">
         <span>Salir</span>
      </a>  
    </li>
</ul>

<script src="JS/jquery.js" type="text/javascript"></script>
<script src="JS/jquery-1.11.3.min.js" type="text/javascript"></script>

I hope you can help me. Thank you.!!

Igor Quirino
  • 1,187
  • 13
  • 28
  • Two requests: please use only English, and only use Code Snippets for full HTML/CSS/Javascript code that can work inside the browser. For all other types of code use the **{}** button, which is the same is using Ctrl-K. – Peter B Nov 23 '16 at 16:13
  • @Peter B, i already edited and a review is pending, ok? – Igor Quirino Nov 23 '16 at 16:21
  • Template.Master/ReiniciarSession is not a valid URL. Response.Redirect will not work if you are using $.ajax. Use [window.location](http://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-page-in-jquery) – Jeroen Heier Nov 23 '16 at 16:27

1 Answers1

0

As @JeroenHeier noted, Template.Master/ReiniciarSession points to a master page, and those are used by actual .aspx pages to render, they're not browsable. You have to implement the ReiniciarSession WebMethod in a specific page, like Login, instead of the Master page.

Then change the AJAX call to that page:

function ReiniciarSession() {
    $.ajax({
        ...
        url: "/Login.aspx/ReiniciarSession",
        ...
    });
}

As the session is independent of which page you're browsing, the effect will be the same.

Marco Scabbiolo
  • 7,203
  • 3
  • 38
  • 46