2011-04-06 15 views
6

Saludos,jquery ajax tiene problemas para conseguir valor de retorno desde el manejador de ashx

No importa lo que haga no puedo conseguir mi código jquery ajax para obtener una respuesta que no sea nula desde una página manejador ashx.

Aquí está mi página de HMT:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>:: ashx tester ::</title> 
    <link rel="stylesheet" href="js/jqueryui/1.8.6/themes/sunny/jquery-ui.css" 
     type="text/css" media="all" /> 
    <script type="text/javascript" src="js/jquery/1.4.3/jquery.min.js"></script> 
    <script type="text/javascript" src="js/jqueryui/1.8.6/jquery-ui.min.js"></script> 
    <script type="text/javascript" src="js/json2/0.0.0/json2.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $('#btnSubmit').click(
       function() { 
        DoIt(); 
       } 
      ); 
     }); 

     function DoIt() { 
      $('#btnSubmit').hide(); 
      $.ajax({ 
       type: "POST",     
       url: "http://localhost:49424/Handler1.ashx", 
       data: { 
        firstName: 'Bob', 
        lastName: 'Mahoney' 
       }, 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       success: function (response) { 
        alert('success: ' + response); 
        $('#btnSubmit').show(); 
       }, 
       error: function (response) { 
        alert('error: ' + response); 
        $('#btnSubmit').show(); 
       } 
      }); 
     } 
    </script> 
</head> 
<body> 
    <input id="btnSubmit" type="button" value="Submit" /> 
</body> 
</html> 

Y aquí es mi página ashx:

Imports System.Web 
Imports System.Web.Services 
Imports System.Collections.Generic 
Imports System.Linq 
Imports System.Data 
Imports System.Configuration.ConfigurationManager 
Imports System.Web.Services.Protocols 
Imports System.Web.Script.Serialization 

Public Class Handler1 
    Implements System.Web.IHttpHandler 

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
     Dim j As New System.Web.Script.Serialization.JavaScriptSerializer 
     Dim s As String 

     s = j.Serialize(Now) 
     context.Response.ContentType = "application/json" 
     context.Response.Write(s) 

    End Sub 

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable 
     Get 
      Return False 
     End Get 
    End Property 

End Class 

Alguna pista?

Gracias!

de Dave

Respuesta

2

Trate url: "/Handler1.ashx",

en lugar de

url: "http://localhost:49424/Handler1.ashx", 
+0

bien ... aquí es la parte extraña. Eso funcionó pero no realmente. Necesito que la página pueda llamar a mi manejador de ashx de forma remota. Pero para probar, copié mi página en mi proyecto e hice su cambio sugerido. ¡Funcionó! Entonces probé la página remota nuevamente, pero esta vez en IE ... He estado probando la página remota usando Chrome. Desactiva la página remota La llamada ajax funciona en IE pero no funciona en Chrome o Firefox. – Dave

1

Yo sólo uso el tipo de datos de texto con los manipuladores

 var webServiceURL = 'http://localhost:12400/Handler1.ashx'; 
     var data = "Key:Value"; 

     alert(data); 

     $("input[id='btnSubmitLead']").click(function() { 
      $.ajax({ 
       type: "POST", 
       url: webServiceURL, 
       data: data, 
       dataType: "text", 
       success: function (results) { 
        alert("Your information has been sent to the dealer, thank you"); 
       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
        alert(textStatus +"-"+ errorThrown); 
        alert("Your information has not been sent"); 
       } 
      }); 
     }); 

Por lo tanto, Recibo información para el controlador en todos los navegadores, pero no recibo la confirmación adecuada de vuelta, ya que siempre es un error. Estoy usando una página html para enviar la llamada ajax. También quité:

  context.Response.ContentType = "application/json" 

de mi manejador que aumentó el trabajo en el lado del servidor, sino también la llamada al servidor ha estado bien ... es la parte de atrás de retorno que me ha dado problemas.

+0

Gracias. Funcionó para mí – Nizam

0

Este código simplificado que funciona para mí ..

$.ajax({ 
     type: "POST", 
     url: "AddProviderToCall.ashx", 
     data: {ProviderID: strProviderID, PhyUserID: PhyUserID }, 
     success: function (response) { 
      alert('success: ' + response); 
     }, 
    }); 

En mi C# Gestor ..

{ 
    context.Response.ContentType = "text/plain"; 
    context.Response.Write(result); 
} 
Cuestiones relacionadas