2012-06-28 76 views
1
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); 
      } 
     } 

    }); 

我调试的问题:将字符串传递到AJAX/Json的

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

是逗号搞乱字符串?

+0

你的数据是一个字符串,而不是一个数组,因此它可能不会被转换为你期望的json。 –

回答

3

您可能需要序列化之前包裹在一个对象的值,ASP.NET知道什么值称为:

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

ASP.NET经常使用的键名,以确定哪些参数值分配给(URL为index.aspx/s假设WebMethod):

[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) ...