2010-07-28 25 views
3

我有按鈕和jQuery腳本(啓動進度條)一個問題:ASP.NET使用jQuery的Ajax功能

<script src="../_Files/JScripts/jquery-1.3.2.js" type="text/javascript"></script> 
<script src="../_Files/JScripts/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script> 

var intervalID; 
$("#<%=this.Button1.ClientID%>").click(
      function() { 

       intervalID = setInterval(updateProgress, 500); 

       $.ajax({ 
        type: "POST", 
        url: "CustomerImport.aspx/ExecuteImport", 
        data: "{}", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        async: true, 
        success: function() 
        { 
         $("#progressbar").progressbar("value", 100); 
         clearInterval(intervalID); 
         $("#result").text('ok'); 
        } 
       }); 

       return false; 
      } 
     ); 

    function updateProgress() { 

      $.ajax({ 
       type: "POST", 
       url: "CustomerImport.aspx/GetProgress", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       async: true, 
       success: function(msg) { 
        $("#result").text = msg.d; 
        var value = $("#progressbar").progressbar("option", "value"); 
        if (value < 100) { 
         $("#progressbar").progressbar("value", msg.d); 
         $("#result").text(msg.d); 
        } 
        else { 
         clearInterval(intervalID); 
         window.location = window.location; 
        } 
       } 
      }); 
     } 

方法:

[System.Web.Services.WebMethod] 
    public void ExecuteImport() 
    { 
     _Presenter.ExecuteImport(); 
    } 

的問題是,該方法不被調用。爲什麼?

當我更換$.ajax爲e.g alert('ok');警報顯示,所以它的工作原理

回答

3

你裝飾你的服務類與[ScriptService]屬性?還可以嘗試將數據參數更改爲:data: { }FireBug對此有何評論?是否有請求被髮送?如果是,服務器響應什麼?

另外你的url有錯(web服務有ASMX擴展)。您寫道:

CustomerImport.aspx/ExecuteImport 

雖然它應該是:

CustomerImport.asmx/ExecuteImport 

這裏,你可以適應您的需求提供完整的工作示例:

Web服務:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[ToolboxItem(false)] 
[ScriptService] 
public class CustomerImport : WebService 
{ 
    [WebMethod] 
    public void ExecuteImport() 
    { 
    } 
} 

呼叫網頁:

<%@ Page Language="C#" %> 
<!doctype html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Test</title> 
    <script type="text/javascript" src="scripts/jquery-1.4.1.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $.ajax({ 
       type: 'POST', 
       url: '/CustomerImport.asmx/ExecuteImport', 
       data: { }, 
       success: function() { 
        alert('ok'); 
       } 
      }); 
     }); 
    </script> 
</head> 
<body> 

    <form runat="server"> 

    </form> 

</body> 
</html> 
+0

螢火說:未知Web方法GetProgress.Parameter名稱:方法名 – Tony 2010-07-28 09:04:45

+0

你能顯示功能的UpdateProgress? – 2010-07-28 09:11:44

+0

它在上面,在我的第一篇文章 – Tony 2010-07-28 09:15:08

1

將錯誤函數添加到ajax調用中...希望您能得到一些信息,以瞭解調用失敗的原因。

你使用的是螢火蟲嗎?觀看網絡標籤。

$.ajax({ 
    type: "POST", 
    url: url, 
    async: false, 
    data: jsonEncodedParams, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (msg) { 

    }, //success 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     if (textStatus == "timeout") { 
     alert('The request timed out, please resubmit'); 
     } //if 
     else { 
     alert(errorThrown); 
     } //else 
    } //error 
    }); //ajax 
1

因爲你的服務器端的端點是 「頁方式」,it must be declared as static

[System.Web.Services.WebMethod] 
public static void ExecuteImport() 
{ 
    _Presenter.ExecuteImport(); 
} 
+0

誰負責CustomerImport.asmx/ExecuteImport轉到page.asmx並執行Excute函數?哪個模塊?你能提供一個鏈接嗎 ? – 2012-07-26 19:02:21

+0

我不明白這個問題。具體的ASP.NET HttpModule? – 2012-07-26 22:59:25

+0

當yu在互聯網上請求一個URI時,通過它的URL,它進入一個頁面,而不是一個方法。那麼誰來負責從URL中提取頁面,然後轉到也在URL中提供的Method。你正在談論你(優秀)網站的相關信息。我問這個問題,因爲我在你的網站上看到了帶有靜態Asp.net函數的Jquery Ajax示例。 – 2012-07-27 13:22:24