X-editable with .Net and c# web methods

asp.net, jquery, webmethod

Solution

If you want to call a page method, first of all you need to make a request of type POST (also having content-type set will not do any harm):

$('#username').editable({
    url: function (params) {                      
        return $.ajax({
            type: 'POST',
            url: 'Default.aspx/TestMethod',
            data: JSON.stringify(params),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            async: true,
            cache: false,
            timeout: 10000,
            success: function (response) {
                alert("Success");
            },
            error: function () {
                alert("Error in Ajax");
            }
        });
    }
});

Also the JSON will be auto deserialized on server side, so you should expect the `name`, `pk` and `value` parameters on server side (this is what plugin is sending according to docs)

[System.Web.Services.WebMethod]
public static String TestMethod(string name, string pk, string value)
{
    //access params here
}

In your case the `pk` will be null as you haven't set one.

Problem

I am using X-Editable Plugin in Asp.net. I have tried this: Usage with .Net and C# Webmethods But it gives me error. It is not calling the WebMethod as it should be. How to solve this problem? Please help. Javascript: ``` $('#username').editable({ url: function (params) { return $.ajax({ url: 'Default.aspx/TestMethod', data: JSON.stringify(params), dataType: 'json', async: true, cache: false, timeout: 10000, success: function (response) { alert("Success"); }, error: function () { alert("Error in Ajax"); } }); } }); ``` HTML: ``` <a href="#" id="username" class="myeditable">superuser</a> ``` WebMethod in Default.aspx: ``` [System.Web.Services.WebMethod] public static String TestMethod(String params) { //access params here } ```

Original source