【问题标题】:Serialized JSON acting strange when calling webservice调用 web 服务时,序列化的 JSON 表现得很奇怪
【发布时间】: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


【解决方案1】:

将您的代码更改为以下内容:

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

【讨论】:

  • 我明白你在说什么。但是有没有一种不同的方式来传递参数,类似于我所拥有的。我需要添加更多参数,不需要是 JSON.stringify?
  • 其他参数是什么类型的数据?
  • 字符串。所以像这样; $.getJSON(conString + 'checkSelectedDate?callback=?', { locationID: locID, orderMode: orderMode, timeOptionsValues: JSON.stringify(timeOptsValueArray), timeOptionsText: JSON.stringify(timeOptsTextArray) }
  • 如果其他数据都是字符串,对它们调用json.stringify不会修改它们
  • 我忽略了其他参数并尝试了您的确切示例。他们给我留下了不调用网络服务的同样问题,除非我取消注释我在循环外填充它的行。
猜你喜欢
  • 2014-12-12
  • 1970-01-01
  • 2020-05-03
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2012-11-11
相关资源
最近更新 更多