简体   繁体   中英

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

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM