0

我不知道我的数据语法是否错误,但是当没有传递参数时这个相同的脚本正在工作,但是当传递参数时,它就不起作用了。为什么?

 <script type="text/javascript" src="jquery-1.7.2.min.js"> 
      <script type="text/javascript">
       function GetAge() {
           var StrYear = document.getElementById("StringYear").value;
           var StrMonth = document.getElementById("StringMonth").value;
           var StrDay = document.getElementById("StringDay").value;
           jQuery.support.cors = true;
           $.ajax({
               type: "POST",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               jsonp: 'jsonp_callback',
               async: false,
               url: 'http://localhost:50113/Service1.asmx/GetAge',
               data: "{ StrYear: "+StrYear+", StrMonth:"+ StrMonth+"  , StrDay: "+StrDay+"  }",
               success: function (msg) {
                   $('#divToBeWorkedOn').html(msg.d);
               },
               error: function (e) {
                   $('#divToBeWorkedOn').html("Unavailable");
               }
           });
         }
        </script> 
4

3 回答 3

5

可能您应该将数据作为对象传递,因此您可能不需要

data: "{ StrYear: "+StrYear+", StrMonth:"+ StrMonth+"  , StrDay: "+StrDay+"  }",

data: { StrYear: StrYear, StrMonth: StrMonth, StrDay: StrDay },
于 2012-08-21T05:38:27.360 回答
1

以这种格式传递您的数据

   data: '{ "StrYear": "' + StrYear+ '","StrMonth":"' + StrMonth+ '" ,"StrDay": "' + StrDay+ '" }',
于 2012-08-21T05:37:53.450 回答
1

尝试dataType: "jsonp",代替dataType: "json",

data: { StrYear: StrYear, StrMonth: StrMonth, StrDay: StrDay },
于 2012-08-21T05:58:24.993 回答