0

I need to be able to pass both a string array and an array of string arrays to a webservice via AJAX in my ASP.NET application. But I keep getting

{"Message":"Invalid object passed in, : or } expected. (16): { basicInfo:\"[\"TESTCO\",\"TESTCO\",\"PO BOX 0\",\"My City\",\"MyState\",\"My Zip\",\"MyPhone\",\"TEST1 -  TEST, TEST3 - TEST, \",\"11/15/2012\",\"16:36\",\"myname\",\"1202160021\"]\"}","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

I don't have a great grasp on JSON and serialization and all of that. So I really don't know what I"m doing wrong.

var cvwOther = new Array();
var equipAssign = new Array();
var qAndA = new Array();
var binfo = new Array(storeinfo[2], storeinfo[6], storeinfo[7], storeinfo[8], storeinfo[9],
                              storeinfo[10], storeinfo[11], storeinfo[12], getToday(), getNow(), userinfo[0], call);

function submitCVW(call) {
      db.transaction(function (tx) {
                    tx.executeSql("Select * From CVWOther Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            cvwOther[i] = result.rows.item(i);
                            i++;
                        });
                    }, onSQLError);
                    tx.executeSql("Select * From EquipAssign Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            equipAssign[i] = result.rows.item(i);
                            i++;
                        });
                    }, onSQLError);
                    tx.executeSql("Select * From CVW Where CallNumber=?", [call], function (tx, result) {
                        var i = 0;
                        $.each(result.rows, function () {
                            qAndA[i] = result.rows.item(i);
                            i++;
                        });

                        $.ajax({
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json",
                            url: "MyService.asmx/CVW",
                            data: '{ basicInfo:"' + JSON.stringify(bInfo) + '", cvwOther:"' + JSON.stringify(cvwOther) + '"}',
                            success: function(data) {
                            },
                            error: function(xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                alert(err.Message);
                            }
                        });
                    }, onSQLError);
                });
            }

Web Method Signature

[WebMethod]
public void SubmitCVW(string[] basicInfo, string[][] cvwOther)
slashingweapon
  • 11,007
  • 4
  • 31
  • 50
jmease
  • 2,507
  • 5
  • 49
  • 89
  • Have you looked at this [post](http://stackoverflow.com/questions/2013728/passing-javascript-array-to-php-through-jquery-ajax/2013784#2013784)? – Blake Plumb Nov 15 '12 at 22:00

1 Answers1

0

The main difference between JSON and javascript literals is that your object attribute names need to be quoted:

{
    "attr_1" : "whatever", // VALID JSON
    attr_2 : "something"   // INVALID JSON
}

Having said that, there is absolutely no reason you should need to build a JSON string when you have the JSON.stringify() method available to you. Instead, build the data you want to encode, and then use the stringify() method on it.

// Why do this ...
data: '{ basicInfo:"' + JSON.stringify(bInfo) + '", cvwOther:"' + JSON.stringify(cvwOther) + '"}',

// ... when you can do this?
data: JSON.stringify( {basicInfo:bInfo, cvwOther:cvwOther} );
slashingweapon
  • 11,007
  • 4
  • 31
  • 50
  • Thanks. That at least resolved the exception I was getting. Still not having any luck getting the array to go through. Now getting "No paramaterless constructor defined for type System.String[]". – jmease Nov 16 '12 at 14:58
  • Changed the string[] argument to take String[] instead and it fixed that specific issue. Not sure how to handle the String[][] though. It is being sent as Object[object][object] but I can't find a corresponding c# data type that works. – jmease Nov 16 '12 at 20:26