无法使用ajax调用将值传递给Web服务

时间:2012-08-21 05:33:48

标签: javascript jquery html json

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

 <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> 

3 个答案:

答案 0 :(得分:5)

可能你应该将数据作为对象传递,所以可能你不需要

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

但是

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

答案 1 :(得分:1)

以这种格式传递您的数据

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

答案 2 :(得分:1)

尝试dataType: "jsonp",而不是dataType: "json",

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