-1

I am trying to use the response.redirect command in call webmethod and it does not work, see how is my code:

HTML:

<a onclick="proximaAula()">Próxima Aula -></a>

JS:

 function proximaAula() {
    var tipo = getParameterByName('t');
    if (tipo == "1") {
        //PageMethods.MyMethod(projekktor('player_a').getPosition(), projekktor('player_a').getDuration(), getParameterByName('codaula'));
        PageMethods.NextAula(projekktor('player_a').getPosition(), projekktor('player_a').getDuration(), getParameterByName('codaula'));
    }
    //alert(tipo);
    if (tipo == "3") {
        var iframe = document.getElementById('viewer');
        var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
        var pag;
        var npag;
        if (iframeDocument) {
            elem = iframeDocument.getElementById('pageNumber').value;
            npag = iframeDocument.getElementById('numPages').innerHTML;
            npag = npag.substring(3, npag.length);
        }
        //return "asasdas"; // 
        //alert(elem + " - " + npag);
        PageMethods.NextAula(elem,npag,getParameterByName('codaula'));
    }
    if (tipo == "4") {
        PageMethods.NextAula("-1","-1",getParameterByName('codaula'));
    }
}

C#:

[WebMethod]
    public static string NextAula(string tempo, string tempomax, string codaula)
    {
        escolawebEntities DB = new escolawebEntities();
        string link = "";
        int icodaula = int.Parse(codaula);

        eadaulaaluno eaula = (from x in DB.eadaulaaluno where x.codeadaula == icodaula select x).FirstOrDefault();

        tempo = tempo.Substring(0, tempo.IndexOf('.'));
        tempomax = tempomax.Substring(0, tempomax.IndexOf('.'));

        int ntempo = ((int.Parse(tempo) * 100) / int.Parse(tempomax));

        if (tempo == tempomax || eaula.percentual == eaula.percentualmax || ntempo >= 95 )//ou perc ser maior 98%
        {
            eadaula aaula = (from x in DB.eadaula where x.eadcodaula == icodaula select x).FirstOrDefault();
            eadaula paula = (from x in DB.eadaula 
                             where x.eadcodcursomodulo == aaula.eadcodcursomodulo
                             where x.ordem == aaula.ordem + 1
                             select x).FirstOrDefault();

            if (paula != null)
            {
                //link = "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia;
                HttpContext.Current.Response.Redirect("eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia,false);
                //Context.Response.StatusCode = 307;
                //Context.Response.AddHeader("Location", "<redirect URL>");

               // HttpContext.Current.Response.StatusCode = 307;

               // HttpContext.Current.Response.AddHeader("Location", "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia);
            }
                //HttpContext.Current.Response.Redirect("eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia);
                //link = "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia;
        }
        return "";
    }

I can not use a command to redirect the page, I've tried several ways, such as the return link via string etc ...

This code works as follows: I click the link, it calls a javascript function, this function takes some information of some components of the page and sends it to serverside by a webmethod.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
ISFO
  • 135
  • 2
  • 9
  • Are you trying to call a web service from JS? Web service does not have Response.Redirect. – Piyush Parashar Nov 21 '14 at 17:52
  • 2
    Instead of having the WebMethod redirect the page, try returning the address that you want to redirect to as part of your response, then have the client read the response to get the URL to redirect to, then have the client access the new URL. – mason Nov 21 '14 at 17:53
  • `Change this line in your code ` PageMethods.NextAula(elem,npag,getParameterByName('codaula'));` to `var strRedirect = PageMethods.NextAula(elem,npag,getParameterByName('codaula'));` then in your WebMethod change the return from return `""` to return the actual string value that you assign to a local variable – MethodMan Nov 21 '14 at 17:55
  • so assign `link = "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia"; then return ""` should be `return link` – MethodMan Nov 21 '14 at 17:58
  • var strRedirect = PageMethods.NextAula (elem, NPAG, getParameterByName ('codaula')); return unefined – ISFO Nov 21 '14 at 19:17

2 Answers2

1

Either if you are calling an async method, or redirecting based on values on the website, you can use this line to redirect the website:

document.location.href = url;

Instead of using redirect on your server code, send the link directly from [WebMethod] -NextAula and use it on the js side.

"eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia" , return this value.

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
RobertoNovelo
  • 3,347
  • 3
  • 21
  • 32
0

A webmethod can not redirect a website to a different address using Response.Redirect. It will send an XML Response back to the caller. Try to send required information from the webmethod and then use it to redirect your page.

Piyush Parashar
  • 866
  • 8
  • 20
  • I need the server to check the information in the database, but the only way to do this without triggering the postback is using webmethod, as I do to the webmethod return a data for clientside? – ISFO Nov 21 '14 at 19:27
  • @ISFO, that's not true. There are several alternatives to WebMethod (which is no longer supported). [Web API](http://www.asp.net/web-api) is one of the easiest to use at the moment. – mason Nov 21 '14 at 20:19