2011-04-06 39 views
6

问候,jQuery的阿贾克斯从ashx的处理程序得到的返回值

不管我做什么,我不能让我的jQuery AJAX代码从一个ASHX处理程序页面null以外的响应的问题。

这是我的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> 

这里是我的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 

任何线索?

谢谢!

戴夫

回答

2

尝试 网址: “/Handler1.ashx”,

代替

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

OK ...这里是怪异的一部分。这有效,但不是真的。我需要页面能够远程调用我的ashx处理程序。但为了测试,我将我的网页复制到了我的项目中,并提出了建议的更改。有效!所以然后我再次尝试远程页面,但这次在IE中......我一直在使用chrome测试远程页面。 发现远程页面ajax调用在IE中工作,但它不适用于Chrome或Firefox! – Dave 2011-04-06 14:33:45

1

我只使用与处理

 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"); 
       } 
      }); 
     }); 

文本数据类型,因此,我在所有浏览器中获得了处理程序的信息,但是我没有得到正确的证实因为这总是一个错误。我正在使用html页面来提交ajax调用。我也删除:

  context.Response.ContentType = "application/json" 

从我的处理程序,其增加在服务器端的工作,而且还调用服务器一直正常...它已经给了我的问题返回回来。

+0

谢谢。它为我工作。 – Nizam 2014-08-15 16:32:23

0

这种简化代码适用于我..

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

在我的C#处理..

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