[英]Web method not called from ajax post
我試圖從ajax像Web方法一樣調用方法:
$.ajax({
url: 'LifeStyleManager.aspx/AddSelfEntry',
method: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: angular.toJson(categories),
//data: angular.copy(categories)
還嘗試將數據作為“ {'items':'“ + angular.toJson(categories)+”'}“
序列化為
{
"items": "[{\"name\":\"Fruits\",\"metrics\":\"cups\",\"entry\":0,\"recommended\":true,\"color\":\"#989898\"},{\"name\":\"Vegetables\",\"metrics\":\"cups\",\"entry\":1,\"recommended\":true,\"color\":\"#37A0BC\"},{\"name\":\"Whole Grains\",\"metrics\":\"cups\",\"entry\":1,\"recommended\":true,\"color\":\"#37A0BC\"},{\"name\":\"Fast Foods\",\"metrics\":\"times\",\"entry\":0,\"recommended\":false,\"color\":\"#989898\"},{\"name\":\"Sweets\",\"metrics\":\"times\",\"entry\":0,\"recommended\":false,\"color\":\"#989898\"},{\"name\":\"Sugary Drinks\",\"metrics\":\"times\",\"entry\":0,\"recommended\":false,\"color\":\"#989898\"}]"
}
在這里,類別被序列化為
[
{
"name": "Fruits",
"metrics": "cups",
"entry": 0,
"recommended": true,
"color": "#989898"
},
{
"name": "Vegetables",
"metrics": "cups",
"entry": 1,
"recommended": true,
"color": "#37A0BC"
}
]
Webmethod就像:
[WebMethod(true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string AddSelfEntry(List<Entry> items)
{
這里的條目是
public class Entry
{
public string name;
public string metrics;
public int entry;
public bool recommended;
public string color;
}
我在控制台出現錯誤:
在調試模式下,webmethod不會遇到斷點。
更新 :我從另一個HTML頁面而不是Web方法位於代碼頁面后面的aspx頁面調用ajax。 是原因嗎?
請幫我哪里錯了?
您的jQuery版本是什么?
類型 (默認:
'GET'
)類型: 字符串
method
的別名。如果您使用的是1.9.0之前的jQuery版本,則應使用
type
。
代替method
,嘗試在AJAX中type: "POST"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.