Javascript 将字符串传递给AJAX/Json
我将问题调试为:Javascript 将字符串传递给AJAX/Json,javascript,jquery,asp.net,ajax,json,Javascript,Jquery,Asp.net,Ajax,Json,我将问题调试为: 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",
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);
}
}
});
逗号是否弄乱了字符串?在序列化之前,可能需要将值包装到对象中,以便ASP.NET知道该值的名称:
cannot convert object of type 'system.string' to type 'system.collections.generic.idictionary 2 system.string system.object '
ASP.NET通常使用键名来确定要为URL为index.aspx/s的WebMethod赋值的参数:
此外,如果目标是一个集合,那么数据也可以是一个数组:
在序列化之前,您可能需要将值包装到对象中,以便ASP.NET知道该值的名称:
cannot convert object of type 'system.string' to type 'system.collections.generic.idictionary 2 system.string system.object '
ASP.NET通常使用键名来确定要为URL为index.aspx/s的WebMethod赋值的参数:
此外,如果目标是一个集合,那么数据也可以是一个数组:
它接受一个对象参数,而不是字符串
它接受一个对象参数,而不是字符串 您的数据是字符串,而不是数组,因此可能不会转换为您期望的json。您的数据是字符串,而不是数组,因此可能不会转换为您期望的json。
[WebMethod]
public static object s(string csv) ...
data = { ids: [192, 273, 182, 347, 13, 34, 52, 2524] };
// then...
[WebMethod]
public static object s(IEnumerable<int> ids) ...
data = "192,273,182,347,13,34,52,2524";
var jsondata = $.toJSON(data);