【发布时间】:2016-02-02 19:10:16
【问题描述】:
我在 C# 中有一个 Web 服务,可以由客户端脚本调用。我使用 JSON 来调用 web 服务。
我向 Web 服务传递了两个数组(通过 JSON.stringify)。
我正在尝试向服务传递来自 html 选择元素的选项列表。它工作正常,并在尝试传递一组值时调用网络服务器。一旦我添加了第二个数组(相同的数组),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) {
});
请阅读我的 cmets。我不明白为什么让人口脱离循环解决了这个问题。我检查了萤火虫,当填充到循环中时,两个数组都是相同的。
服务器端:
[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,我也会遇到同样的问题。
【问题讨论】:
-
您遇到了什么问题?
-
@DelightedD0D JSON 请求未调用 Web 服务
-
我们能看到选择框的html吗?
-
@DelightedD0D 为您更新了我的问题,先生
-
也许这只是一个错字,但在
$.each中,您在两个阵列上都在做push(this.value),而在工作注释行中,您正在使用innerHTML
标签: javascript c# jquery json web-services