0

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;
}
0

2 Answers 2

1

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...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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,

2 Comments

What about how I receive it in the web method?
You should change your current static function into an Action. public ActionResult getEventLetter(string var1, string var2) { .. } (Naming convention for the action is in order).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.