2012-06-25 210 views
0

我正在使用VisualStudio 2008並在ASPX頁面上有一個方法我正嘗試使用jQuery使用jQuery調用,如下所示。我只是將頁面的HTML返回。 webmethod沒有被調用。有趣的是,如果我更改webMethod的名稱來調用JavaScript,我仍然會返回HTML。不是說webMethod無法找到的錯誤。使用jQuery調用webMethod返回HTML ... webMethod根本沒有調用

我試着改變數據參數爲「{'dummy':0}」,但這並沒有幫助。

我已經在新的VS 2010應用程序中沒有問題地使用這個策略,但似乎無法讓它在VS 2008中的現有應用程序上工作,我正在添加一個頁面。 (試圖給舊應用程序添加一個扭曲)我已經看過firefox在firefox中告訴我的是什麼,並且看起來都是正確的。

任何幫助,非常感謝。

C#的WebMethod decalaration:

[WebMethod()] 
    public static string getQuestionnaires(int dummy) 
    { 
     System.Diagnostics.Debug.WriteLine("getQuestionnaires called"); 
     SqlCommand command = new SqlCommand(); 
     command.CommandText = "dbo.ws_GetPSQuestionnaire"; 
     command.CommandType = CommandType.StoredProcedure; 
     DataTable dtQuestionnairesRaw = Utilities.ReturnDataSet(command).Tables[0]; 

     DataTable dtQuestionnaires = new DataTable(); 
     dtQuestionnaires.Columns.Add(new DataColumn("questionnaireID", typeof(int))); 
     dtQuestionnaires.Columns.Add(new DataColumn("name")); 

     foreach (DataRow dr in dtQuestionnairesRaw.Rows) 
     { 
      DataRow drNew = dtQuestionnaires.NewRow(); 
      drNew["questionnaireID"] = dr["questionnaireID"]; 
      drNew["name"] = Utilities.RemoveHTMLTags(dr["name"].ToString()); 
      dtQuestionnaires.Rows.Add(drNew); 
     } 
     dtQuestionnaires.AcceptChanges(); 

     return (JsonConvert.SerializeObject(dtQuestionnaires, Formatting.Indented)); 
    } 

我與這個JavaScript調用它。我的錯誤函數總是被調用。

$(document).ready(function() { 
     var request = $.ajax({ 
      type: "POST", 
      url: "/crs4/admin/editPSQuestionnaire.aspx/getQuestionnaires", 
      contentType: "application/json; charset=utf-8", 
      data: "{ 'dummy':'0' }", 
      dataType: "json", 
      success: populateQuestionnaires, 
      error: AjaxFailed 
     }); 
    }); 
+0

「{ '啞': '0'}」, 應該是在字符串中有效的JSON符號「{ 「假」:「0 「}', - 您還必須設置特定的web.config選項來調用頁面方法(而不僅僅是Web服務方法)。你能解釋一下爲什麼你手動序列化和讓.net通過返回一個有效的項目如列表來爲你做? –

+0

我不確定你的意思。我不需要將有效的JSON傳遞給Web方法嗎?否則我將如何通過它?一旦這個工作正常,我將有其他方法,我希望用不同的參數相應地調用。 –

+0

我發佈的內容僅僅是爲了讓它起作用。我將使用一個函數,該函數接受一系列參數供我的頁面中的其他地方使用。 –

回答

1

讓我們下來很簡單,以驗證您的配置:

創建一個簡單的類返回:

public class myReturn 
{ 
    /// web service/webmethod needs 0 parameter constructor 
    public myReturn() 
    { 
    } 
    public myReturn(string returnValue) 
    { 
     ReturnValue = returnValue; 
    } 
    public string ReturnValue; 
} 

聲明你的Web方法使用類:

[WebMethod()]  
public static myReturn getQuestionnaires(int dummy)  
{ 
    return new myReturn("howdy"); 
} 

稱呼:

//set up the ajax with error so we see what is going on. 
// the following syntax requires jquery 1.5 or later for the 
// converters used for asp.net 
$.ajaxSetup({ 
    data: "{}", 
    dataType: "json", 
    type: "POST", 
    contentType: "application/json", 
    converters: { 
     "json jsond": function(msg) { 
      return msg.hasOwnProperty('d') ? msg.d : msg; 
     } 
    }, 
    error: function(xhr, textStatus, errorThrown) { 
     var errorMessage = ("Ajax error - " + this.url + " | " 
      + textStatus + " | " + errorThrown + " | " 
      + xhr.statusText + " | " + xhr.status); 
     alert(errorMessage); 
    } 
}); 

var pString = '{"dummy":0}'; 
$.ajax({ 
    data: pString, 
    url: "/crs4/admin/editPSQuestionnaire.aspx/getQuestionnaires", 
    success: function(msg) { 
     alert(msg); 
    } 
}); 

編輯:您可能需要這在web配置:

<httpModules> 
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
</httpModules> 
+0

好吧,我創建了一個新的空白頁面來對稱爲「測試器」運行此測試。改爲改爲使用該頁面的URL。出現此錯誤:Ajax錯誤 - /crs4/admin/Tester.aspx/getQuestionnaires | parsererror | SyntaxError:JSON.parse:意外字符|好的| 200 –

+0

如果將調試器運行到它返回的行,它是否真的如預期的那樣得到0的方法?或者簡單地把它改成像pString中的另一個數字2,如果這是令人困惑的:) –

+0

此外,請確保你的web.config文件設置爲...調用aspx.net頁面方法 - 大量的文章可用。我假設大約3.5而不是2.0,但如果你使用2.0,你也需要腳本管理器。 –