我設置了一個頁面方法,並試圖通過jQuery調用它。有時我會遇到成功的功能,有時甚至是錯誤功能,對我來說似乎是隨機的。在任何情況下,響應都包含整個頁面標記。不調用頁面方法
我試過使用$.ajax
和ScriptManager
,結果相同。 我也試過這裏的想法:Call ASP.NET PageMethod/WebMethod with jQuery - returns whole page並沒有什麼。
下面是JavaScript代碼:
$(document).ready(function() {
$(".tree").dynatree({
onActivate: function(node) {
$('#title').val(node.data.title);
$.ajax({
type: "POST",
url: window.location.href + "/GetData",
data: "{'ID':22}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) { alert(response); },
error: function() { alert('Error!'); }
});
}
});
});
這裏是C#代碼:
[WebMethod]
public static string GetData(string ID)
{
if (string.IsNullOrEmpty(ID))
throw new Exception("No ID passed!");
return "Test";
}
編輯:嗯,我懂了工作。我將參數類型從int更改爲字符串,現在調用該方法。我可以稍後再做int.Parse,但爲什麼會發生這種情況?
當你使用生成的客戶端代理它是否工作(即PageMethods.GetData(...)?) – davidfowl 2011-05-19 09:45:47
@dfowler - Nope。同樣的結果。 – 2011-05-19 09:47:23
你的路線或任何可能與網址混淆? – davidfowl 2011-05-19 10:34:45