2011-04-06 46 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); 
}