0

I have a webservice in c# which can be called by client script. I use JSON to call the webservice.

I pass the web service two arrays ( via JSON.stringify).

I am trying to pass the service a list of options from an html select element. It works fine, and called the webserive when trying to pass one array of values. As soon as I add a second array (identical array) the webservice json call does not fire.

Client Side:

 // get options from html drop down
   var timeOpts = document.getElementById('ddlTime').options;

    // Placeholders for value and innerHTML
    var timeOptsValueArray = new Array();
    var timeOptsTextArray = new Array();

    // for testing purposes I filled both arrays with the same thing (value)
    $.each(timeOpts, function () {
        timeOptsValueArray.push(this.value);
        timeOptsTextArray.push(this.value); // If I comment this out and uncomment below everything works fine
    });

    // **** If I uncomment this, and comment out the population in the loop, everything works fine? ******
    //timeOptsTextArray = [timeOpts[0].innerHTML, timeOpts[1].innerHTML, timeOpts[2].innerHTML];

        $.getJSON(conString + 'checkSelectedDate?callback=?', { timeOptionsValues: JSON.stringify(timeOptsValueArray), timeOptionsText: JSON.stringify(timeOptsTextArray)}, function (response) {


        });

Please read my comments. I don't understand why taking the population out of the loop resolved the issue. I checked in firebug, and both arrays are identical when populated int he loop.

Server Side:

        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public List<LocationDates> checkSelectedDate( string timeOptionsValues,string timeOptionsText)
        {

            System.Web.Script.Serialization.JavaScriptSerializer serializer1 = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<string> timeOptionValueList = serializer1.Deserialize<List<string>>(timeOptionsValues);
            List<string> timeOptionTextList = serializer1.Deserialize<List<string>>(timeOptionsText);


            List<LocationDates> locationDates = new List<LocationDates>();

            return locationDates;
        }

The HTML is a blank page, with a select element, and the javascript above.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Get Cart</title>
    <script src="http://oops.com/FlashMobile/Scripts/jquery-2.1.4.min.js" type="text/javascript"></script>
    <script src="http://oops.com/FlashMobile/Scripts/bootstrap.min.js" type="text/javascript"></script>
    <script src="http://oops.com/FlashMobile/Scripts/SelectionGenerator.js" type="text/javascript"></script>
    <link href="http://oops.com/FlashMobile/Content/bootstrap.css" rel="stylesheet" type="text/css" />

<!--    <script src ="Scripts/FlashCart.js" type="text/javascript"></script>
    <script src ="Scripts/FlashOrderModes.js" type="text/javascript"></script>-->
    <script src ="Scripts/Checkout.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>

     <script type="text/javascript">

         $(document).ready(function () {

             // First lets load the checkout
             LoadCheckout('testing'); // This calls the javascript above 

         });
     </script>


</head>

<body>

<select class="form-control"  style="width:128px"  id="ddlTime">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
            <option>6</option>
            <option>7</option>
            <option>8</option>
            <option>9</option>
            <option>10</option>
            <option>11</option>
            <option>12</option>
            <option>13</option>
            <option>14</option>
            <option>15</option>
            <option>16</option>
            <option>17</option>
            <option>18</option>
            <option>19</option>
            <option>20</option>
        </select>

</body>
</html>

Am I doing something obviously wrong here? I even if I try to populate timeOptsTextArray in a for loop, I get the same issue.

clamchoda
  • 4,411
  • 2
  • 36
  • 74

1 Answers1

2

Change your code to the below:

 var data = {
   timeOptionsValues: timeOptsValueArray,
   timeOptionsText: timeOptsTextArray
 }
 $.getJSON(conString + 'checkSelectedDate?callback=?',JSON.stringify(data) , function(response) { });

You should build the whole object then call JSON.stringify on that object rather than calling it on the parts then putting them together

Or send the data as a plain object like:

 var data = {
   timeOptionsValues: timeOptsValueArray,
   timeOptionsText: timeOptsTextArray
 }
 $.getJSON(conString + 'checkSelectedDate?callback=?', data, function(response) { });
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • I see what your saying. But is there a different way to pass the parameters, simular to what I have. I need to add in more parameters down the line, that do NOT need to be JSON.stringify? – clamchoda Feb 02 '16 at 19:50
  • What types of data are the other parameters? – Wesley Smith Feb 02 '16 at 19:56
  • String. So like this; $.getJSON(conString + 'checkSelectedDate?callback=?', { locationID: locID, orderMode: orderMode, timeOptionsValues: JSON.stringify(timeOptsValueArray), timeOptionsText: JSON.stringify(timeOptsTextArray) } – clamchoda Feb 02 '16 at 20:00
  • If the other data is all strings, calling json.stringify on them won't modify them – Wesley Smith Feb 02 '16 at 20:04
  • I ignored the other parameters and tried your exact examples. They leave me with the same issue of not calling the webservice, Unless I uncomment the line where I populate it outside the loop. – clamchoda Feb 02 '16 at 20:16
  • Also calling it like this $.getJSON(conString + 'checkSelectedDate?callback=?', JSON.stringify(data), function (response) { }); Passes the parameters to the webservice as null – clamchoda Feb 02 '16 at 20:18
  • Sorry, I dont know much about C#, couldnt you just use the second method above passing the whole plain object and just skip all the `Deserialize` stuff on the server? – Wesley Smith Feb 02 '16 at 20:24
  • I don't think you can pass complex objects to the c# webserive without serialization. Well, it gives me an error saying that when I try anyways lol. :( – clamchoda Feb 02 '16 at 20:27
  • Check out [this article](http://www.aspsnippets.com/Articles/Send-and-receive-JavaScript-Array-to-Web-Service-Web-Method-using-ASP.Net-AJAX.aspx), I didnt go through it all but looks like the methods shown there would work – Wesley Smith Feb 02 '16 at 20:28
  • Or use the first method above and on the server, call `Deserialize` on the whole `checkSelectedDate` variable then get the data from the resultant object – Wesley Smith Feb 02 '16 at 20:33
  • Thank you very much Delighted. But I am actually making an API which requested by client, needs to be 100% client side script interface. So ASPX options which use the scriptmanager, will not work for me. Sorry, I'm a hard nail to hit :( – clamchoda Feb 02 '16 at 20:36
  • No worries, what about my last comment? – Wesley Smith Feb 02 '16 at 20:37
  • Delighted, my actual problem is when the stringify variable is over 1180 characters long it fails. How do I increase this limit? – clamchoda Feb 03 '16 at 00:20
  • @clamchoda, Left an answer [on your other question](http://stackoverflow.com/questions/35166381/json-limited-to-1180-characters/35166488#35166488) – Wesley Smith Feb 03 '16 at 00:40