我在c#中有一个可以通过客户端脚本调用的web服务。我使用JSON来调用webservice。
我将Web服务传递给两个数组(通过JSON.stringify
)。
我正在尝试从html select元素传递服务的选项列表。它工作正常,并在尝试传递一个值数组时调用webserive。只要我添加第二个数组(相同的数组),webservice json调用就不会触发。
客户端:
// 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) {
});
请阅读我的评论。我不明白为什么把人口赶出循环解决了这个问题。我检查了firebug,当循环填充时,两个数组都是相同的。
服务器端:
[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;
}
HTML是一个空白页面,带有一个select元素和上面的javascript。
<!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>
我在做一些明显错误的事吗?即使我尝试在for循环中填充timeOptsTextArray
,我也会遇到同样的问题。
答案 0 :(得分:2)
将您的代码更改为以下内容:
var data = {
timeOptionsValues: timeOptsValueArray,
timeOptionsText: timeOptsTextArray
}
$.getJSON(conString + 'checkSelectedDate?callback=?',JSON.stringify(data) , function(response) { });
你应该构建整个对象然后在该对象上调用JSON.stringify
而不是在部件上调用它然后将它们放在一起
或者将数据作为普通对象发送,如:
var data = {
timeOptionsValues: timeOptsValueArray,
timeOptionsText: timeOptsTextArray
}
$.getJSON(conString + 'checkSelectedDate?callback=?', data, function(response) { });