2011-01-06 60 views
1

我已經使用了很多選項來解決這個問題,但它不起作用。jQuery + ASP.NET 4.0 ajax頁面方法不起作用

我的斷點在Web方法中沒有命中。此外,正在調用成功函數,但是,返回的整個頁面內容不是字符串「hello」。

我的頁面根本沒有使用asp.net ajax scriptmanager。它只使用普通的jQuery。我正在使用ASP.NET 4.0。

總之,我的網頁的方法被定義爲:

[WebMethod] 
public static string Populate() 
{ 
    return "hello"; 
} 

Ajax調用是:

$.ajax({ 
    url:'WebForm3.aspx/Populate', 
    data:{}, 
    dataType:"json", 
    type:"GET", 
    success: function(msg) { 
     alert("success"); 
    }, 
    error: function(msg, text) { 
     alert(text); 
    } 
}); 

回答

0

頁方法只有工作的時候POST版來,像這樣:

$.ajax({ 
    url:'WebForm3.aspx/Populate', 
    data: '{}', 
    dataType:"json", 
    type:"POST", 
    contentType: 'application/json; charset=utf-8', 
    success: function(msg) { 
     alert("success"); }, 
    error: function(msg, text) { 
     alert(text); 
    } 
}); 

不要忘記引用數據參數:data: '{}'