Dennis
Dennis

Reputation: 297

Can't pass values to a webservice using ajax call

I don't know if my data syntax is wrong or not, but this same script when no parameter is passed is working, but when parameter is passed, it is not working. Why?

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

Upvotes: 0

Views: 197

Answers (3)

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

try dataType: "jsonp", instead of dataType: "json",

and

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

Upvotes: 1

Danil Speransky
Danil Speransky

Reputation: 30453

Probably you should pass data as an object, so probably you don't need

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

But

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

Upvotes: 5

Buzz
Buzz

Reputation: 6320

Pass your data in this format

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

Upvotes: 1

Related Questions