Suha Alhabaj
Suha Alhabaj

Reputation: 101

receive JSON variables in Web Method

This is not a new idea in programming but I really need help on how to do it correctly. I am trying to send 2 variables in a JSON call to a web method.

$("#btn1").click(function () {
    getEventL($(this).find(1).val(), "a");
});

function getEventLetter(v1, v2) {
    var nEvent = { var1: v1, var2: v2 };           
    $.ajax({
        type: "POST",
        url: "default.aspx/getEventL",
        data: JSON.stringify(nEvent),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (response) {
      }
    });
}

How can I receive and read these two values in the web method?

Can I do just this?

public static object getEventLetter(string var1, string var2)
{
    // get event
    int _var1= Int32.Parse(var1);
    string _var2= var2;
}

Upvotes: 0

Views: 691

Answers (2)

randomstudious
randomstudious

Reputation: 122

What you are doing seems to be correct. You can simply get the data passed in ajax call by referencing the parameters from the web method as:

public static object getEventLetter(string var1, string var2)
{
 string v1 = var1;
 string v2 = var2;
 //other code...
}

Upvotes: 1

Orel Eraki
Orel Eraki

Reputation: 12196

A. You're using a static object instead of a instance method.

B. You're sending a string instead of your object.

data: JSON.stringify(nEvent),

Should be changed to:

data: nEvent,

Upvotes: 1

Related Questions