views:

185

answers:

3

Hi, i have this webmethod that redirects to a new page from server,

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public static Boolean realizaConsulta(Dictionary<string, string> datos)
{
    System.Web.HttpContext.Current.Response.Redirect("PRepConsulta.aspx", false);
}

but i got this error:

Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'realizaConsulta' failed with the following error: System.InvalidOperationException-- Authentication failed.

i hope anyone can help me!! please

+1  A: 

I'm not sure about the "authentication failed" part, but a Response.Redirect inside of a WebMethod is probably going to break the SOAP client that's calling the method. It's expecting a Boolean, not a redirect. There's a thread on the subject (with alternative suggestions) here:

http://www.dotnet247.com/247reference/msgs/22/113774.aspx

David
For lack of a better place to put this reply... It sounds like you're not actually telling the browser to redirect. However you've wired up the JavaScript to call the web service, it's calling it and receiving something other than a boolean (which is what it expects). The context of the SOAP call to the web service is different than the context of the web page at which you're looking. At no point is the browser being told to redirect to another page, instead it's just calling a SOAP method and receiving an unexpected response. The redirect logic is in the wrong place, basically.
David
A: 

look the other options i have tried:

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public static Boolean realizaConsulta(Dictionary<string, string> datos)
        {
           System.Web.HttpContext.Current.Server.Execute("PRepConsulta.aspx", false);
        }

IT WORKS, BECAUSE, IT GOES TO PRepConsulta.aspx AND EXECUTE THE UNDERCODE, BUT THE PAGE NEVER SHOWS UP.

I HAVE ALSO TRIED:

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public static Boolean realizaConsulta(Dictionary<string, string> datos)
        {
               HttpContext.Current.Server.Transfer("PRepConsulta.aspx", false);
        }

BUT I GOT THIS ERROR:

Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'realizaConsulta' failed with the following error: System.Threading.ThreadAbortException-- Subproceso anulado.

I DONT KNOW WHAT ELSE TO TRY

THANKS FOR ANY HELP

ingrid
How are you calling this WebMethod?
David
im calling it from the client side <script language="javascript" type="text/javascript"> PageMethods.realizaConsulta(Datos); </script>
ingrid
Note: You're jumbling up the answer thread on this page. To elaborate on your question, please either edit the question or add a comment to your question or to a proposed answer.
David
A: 

ok, im gonna organize the application's flow:

from my client, in javascript, im calling a web service

PageMethods.realizaConsulta(Datos);

in my codebehind i have to execute the pageMethod and call another page

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public static Boolean realizaConsulta(Dictionary<string, string> datos)
{

    clsGeneral consulta;
    DataTable dtTemp = new DataTable();

    using (consulta = new clsGeneral("SQLConn"))
    {
        consulta.consultaPrograma(ref dtTemp, datos["Codigo"],  Int16.Parse(datos["Cod_Actividad"]), Int16.Parse(datos["Cod_SubActividad"]), datos["FechaIni"], datos["FechaFin"]);
        HttpContext.Current.Session["Consulta"] = dtTemp;

    //THIS ARE THE 3 DIFFERENT WAYS I HAVE TRIED TO CALL THE PRepConsulta.aspx,
    //I DONT KNOW IF THERE IS A BETTHER WAY TO DO IT

    //System.Web.HttpContext.Current.Response.Redirect("PRepConsulta.aspx", false);
    //HttpContext.Current.Server.Transfer("PRepConsulta.aspx", false);
    //System.Web.HttpContext.Current.Server.Execute("PRepConsulta.aspx",writer, false);
    }
    return true;
}

THANKS

ingrid
The redirect is really just in the wrong place in this design. If the user is _always_ redirected to the other page, then you'll probably want to either: 1) Redirect the user from the JavaScript to a new page and call this logic on that page's load; 2) Place the redirect in the JavaScript after the call to this WebMethod returns. Putting the redirect in the WebMethod doesn't tell the browser to redirect, it just breaks the return value.
David
Well,, thats how I did it in the beggining, i was calling the webMethod and after that, i was calling the new page by window.open function, but, when i uploaded the project into the server, i realized that the window opens before the webMethod ends.The webMethod runs a query and i keep the result into a session variable, and i use it in the window that i open next.thats why i tried to open the window from the server, because i need to be sure, that the query has already run and the result is in my session variable.
ingrid
Sounds like you're calling the WebMethod asynchronously. Surely there's a way to wait for the return, check the value, and respond accordingly (with either a redirect or an error message). Also, is there a specific reason this WebMethod is being called via JavaScript in the first place? There seems to be some bigger picture influencing this design that isn't clear here.
David
ok i solved it, as you said, im excecuting another function based on the response:PageMethods.realizaConsulta(Datos, onLoadDatos);function onLoadDatos(response){ if(response){ window.open('http://www.website.com/');} else{alert("error")}}thanks for your answers!!
ingrid