gpt4 book ai didi

javascript - 调用 Web 服务时序列化 JSON 表现得很奇怪

转载 作者:行者123 更新时间:2023-12-02 15:05:14 25 4
gpt4 key购买 nike

我有一个 C# Web 服务,可以通过客户端脚本调用。我使用 JSON 来调用 Web 服务。

我向 Web 服务传递了两个数组(通过 JSON.stringify)。

我试图向服务传递来自 html select 元素的选项列表。它工作正常,并在尝试传递一组值时调用网络服务器。一旦我添加第二个数组(相同的数组),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 ,我也会遇到同样的问题。

最佳答案

将代码更改为以下内容:

 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) { });

关于javascript - 调用 Web 服务时序列化 JSON 表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35161888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com