2009-05-20 142 views
34

我試圖從客戶端通過jQuery調用服務器端方法。我的代碼如下:通過jQuery調用ASP.NET服務器端方法

服務器端:

using System.Web.Services; 
    [WebMethod()] 
    //[ScriptMethod()] 
    public static void SendMessage(string subject, string message, string messageId, string pupilId) 
    { 
     //Send message 
    } 

客戶端:

$("#btnSendMessage").live("click", function(){ 
    var subject = $("#tbSubject").val(); 
    var message = $("#tbMessage").val(); 
    var messageId = $("#hdnMessageId").val(); 
    var pupilId = $("#hdnPupilId").val(); 

    $.ajax({ 
     type: "POST", 
     url: "./MessagePopup.aspx/SendMessage", 
     data: ("subject=" + subject + "&message=" + message + "&messageId=" + messageId + "&pupilId=" + pupilId), 
     error: function(XMLHttpRequest, textStatus, errorThrown){ 
      alert(textStatus); 
     }, 
     success: function(result){ 
     alert("success"); 
     } 
    }); 
    return false; 
}); 

我已經添加在服務器端的SendMessage方法一個破發點,但它從來沒有擊中它,但是當我運行代碼時,jQuery成功方法被調用。這可能是什麼造成的?'

+0

和SendMessage裏面的代碼沒有運行,或者只是不能調試它? – 2009-05-20 09:05:58

+0

這兩個,SendMessage代碼沒有運行,我無法調試它。 – Fermin 2009-05-20 09:22:06

+0

您的服務器端方法真的是ASPX頁面,而不是ASMX或WCF Web服務?網址指向aspx網頁。 – 2009-05-20 09:30:14

回答

36

要調用ASP.NET AJAX 「ScriptServices」 和第方法,你需要使用完整的$。阿賈克斯()語法:

$.ajax({ 
    type: "POST", 
    url: "MessagePopup.aspx/SendMessage", 
    data: "{subject:'" + subject + "',message:'" + message + ",messageId:'" + messageId + "',pupilId:'" + pupilId +"'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
    // Do something interesting here. 
    } 
}); 

見本後爲詳細說明爲什麼這是必要的:http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

編輯:擴展名不會更改爲.asmx,但仍然.aspx。

2

您應該使用web服務而不是常規的aspx網頁。網頁不支持調用web方法,我相信你的jQuery請求會加載HTML頁面。我建議你兩件事:

  1. 使用Fiddler2(與IE)或HttpFox(與Firefox)來調試客戶端的AJAX請求和響應。
  2. 在服務器端使用WCF Web服務。在這種情況下,您可以使用SvcConfigEditorSvcTraceViewer來配置和調試服務器端的Web方法。
1

以下是可能適用於您的情況的代碼。

<script type="text/javascript"> 
    $(document).ready(function() { 

     // Add the page method call as an onclick handler for the button. 
     $("#btnSubmit").click(function() { 

      //get the string from the textbox 
      $.ajax({ 

       type: "POST", 
       url: "testSearch.aspx/GetMyShippingDate", 
       contentType: "application/json; charset=utf-8", 
       data: "{'tracking_num': '" + $("#txtTrackingNumber").val() + "'}", 
       dataType: "json", 
       success: function (date) { 

        // Replace the div's content with the page method's return. 
        Success(date); 

       }, 
       error: Failed 
      }); 
     }); 
    }); 

    function Success(result) { 
      $("#ParcelShippingDate").html(result.d); 
     } 
     function Failed(result) { 
      alert(result.status + " " + result.statusText); 
     } 

有一個例子是一直爲我工作。

下面是完整的文章http://www.webdeveloperpost.com/Articles/How-to-use-jquery-ajax-in-asp-dot-net-web-page.aspx

它非常適合那些想使用jQuery asp.net方法調用回

2
$.ajax({ 
     type: "POST", 
     url: "MessagePopup.aspx/SendMessage", 
     data: "{subject:'" + subject + "',message:'" + message + ",messageId:'" + messageId + "',pupilId:'" + pupilId +"'}", 
     async: true, 
     cache: false, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function() {}, 
     error:function (xhr, ajaxOptions, thrownError){ alert(thrownError); } 
    }); 

如果這不起作用的一個直接的方式.. 。而給人「語法錯誤:語法錯誤」 ......然後在你的web.config文件中添加和</system.web>這個

<httpHandlers> 
      <remove verb="*" path="*.asmx"/> 
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, 
System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
PublicKeyToken=31BF3856AD364E35"/> 
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, 
System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
PublicKeyToken=31BF3856AD364E35"/> 
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, 
System.Web.Extensions, Version=3.5.0.0, Culture=neutral, 
PublicKeyToken=31BF3856AD364E35" validate="false"/> 
     </httpHandlers> 
     <httpModules> 
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, 
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     </httpModules> 

之間</compilation>

希望這將有助於某人,因爲我花了一段時間來弄清除jquery函數之外,我必須在Web.Config中添加它。

相關問題