gpt4 book ai didi

javascript - 将字符串传递给 AJAX/Json

转载 作者:行者123 更新时间:2023-11-30 10:40:36 26 4
gpt4 key购买 nike

function s() {
data = "192,273,182,347,13,34,52,2524";
var jsondata = $.toJSON(data);
$.ajax({
type: "POST",
url: "index.aspx/s",
data: jsondata,
contentType: "application/json; charset=utf-8",
dataType: "text json",
beforeSend: function (xhr) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");
},
success: function (msg) {
if (msg.d == "OK") {
//WIN!
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (typeof (errorThown) != "undefined")
alert("ERROR: SaveWidgetSettings() " + textStatus + '\n' + errorThrown);
else {
var errdetail = eval("(" + XMLHttpRequest.responseText + ")");
alert("ERROR: SaveWidgetSettings() " + textStatus + '\n' + errdetail.Message);
}
}

});

我将问题调试为:

cannot convert object of type 'system.string' to type 'system.collections.generic.idictionary 2 system.string system.object '

逗号把字符串弄乱了吗?

最佳答案

您可能需要在序列化之前将值包装在一个对象中,以便 ASP.NET 知道该值的名称:

data = { csv: "192,273,182,347,13,34,52,2524" };

ASP.NET 通常使用键名来确定要为哪个参数赋值(假设 WebMethod 的 URL 为 index.aspx/s):

[WebMethod]
public static object s(string csv) ...

此外,如果目标是一个集合,data 也可以是一个Array:

data = { ids: [192, 273, 182, 347, 13, 34, 52, 2524] };

// then...
[WebMethod]
public static object s(IEnumerable<int> ids) ...

关于javascript - 将字符串传递给 AJAX/Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11251261/

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