gpt4 book ai didi

javascript - 未从 ajax post 调用 Web 方法

转载 作者:行者123 更新时间:2023-12-03 09:22:08 27 4
gpt4 key购买 nike

我正在尝试从 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;

}

我在控制台收到错误:

enter image description here

在 Debug模式下,webmethod 没有断点命中。

更新:我从另一个 html 页面而不是 aspx 页面调用 ajax,其中 Web 方法位于代码隐藏中。是这个原因吗?

请帮助我哪里错了?

最佳答案

你的 jQuery 版本是什么?

type (default: 'GET')

Type: String

An alias for method.

You should use type if you're using versions of jQuery prior to 1.9.0.

尝试在 AJAX 中键入:“POST”,而不是方法

关于javascript - 未从 ajax post 调用 Web 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31811499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com