1

Im triying to send out parameter to webmethod. I cant change web method because its not mine. I need to use it with only jquery.

Web method example;

[WebMethod]
public static string getValue(int id, out string name)
{
   name = (id * 5).ToString();
   return "Success";
}

Jquery example;

 $(document).ready(function () {
     getValue();
 });

 function getValue() {
     $.ajax({
         url: "../WebMethods.aspx/getValue",
         type: 'post',
         contentType: "application/json",
         dataType:"json",
         data: JSON.stringify({ id: 0, name: "" }),
         success: function (data) {
             console.log(data);
         }
     });
 }

I need name value in success function. Is it possible? I can call it in c# and its working. Bu I need it to work in ajax method.

cbalakus
  • 620
  • 7
  • 15

1 Answers1

0

You can use in your success method.

$(this).attr('data')

for your case it should be like :

$(document).ready(function () {
 getValue();

});

function getValue() {
     $.ajax({
         url: "../WebMethods.aspx/getValue",
         type: 'post',
         contentType: "application/json",
         dataType:"json",
         data: JSON.stringify({ id: 0, name: "" }),
         success: function (data) {
             console.log(data);


         console.log( $(this).attr('data') );  
         }
     });
 }

---update 2

try this

 var Data = { id: 0 , name : "" };

function getValue() {
     $.ajax({
         url: "../WebMethods.aspx/getValue",
         type: 'post',
         contentType: "application/json",
         dataType:"json",
         data: Data ,
         success: function (data) {
             console.log(data);


         console.log( Data.id );  
         }
     });
 }
Jayanti Lal
  • 1,175
  • 6
  • 18