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.