Using PageMethods with jQuery

Simply put, I want to call public static methods decorated with the WebMethod attribute inside my C # file code from jquery.ajax to get json and other simple things (in different functions). But instead, I get the whole page: '(

I do not use asp.net AJAX, although I am developing a .NET 3.5 environment and using VS 2008. (There are some restrictions from the client)

Please let me know if I can use page methods using asp.net ajax or if not what other simple solution?

+3
source share
4 answers

. jQuery . . jeditable jquery ajax, ajax, :

    var ajaxoptions = {
        type: 'POST',
        data: submitdata,
        url: settings.target,
        success: function(result, status) {
            if (ajaxoptions.dataType == 'html') {
                $(self).html(result);
            }
            self.editing = false;
            callback.apply(self, [result, settings]);
            if (!$.trim($(self).html())) {
                $(self).html(settings.placeholder);
            }
        },
        error: function(xhr, status, error) {
            onerror.apply(form, [settings, self, xhr]);
        }
    };

html , , json. - :

    var ajaxoptions = {
        type: 'POST',
        data: submitdata,
        url: settings.target,
        dataType: 'json', //Data Type
        contentType: 'application/json; charset=utf-8', //Content Type
       //....other settings
    };

, dataType contentType :

    var ajaxoptions = {
        type: 'POST',
        data: submitdata,
        url: settings.target,
        dataType: settings.dataType,
        contentType: settings.contentType,
       //....other settings
    };

: ( ( submitdata​​strong > ) , asp.net json-. jquery json plugin ajax, dataType:

    if (settings.dataType == "json") {
        if ($.toJSON) {
            submitdata = $.toJSON(submitdata); 
        }
    }

!!!

+3

, , WebMethods asp.net AJAX. , , . .

aspx, AJAX .

If Not (Request("method") Is Nothing) Then
            method = Request("method")
            username = HttpContext.Current.User.Identity.Name.ToString
            Select Case UCase(method)
                Case "GETPROJECTS"
                    Response.ContentType = "text/json"
                    Response.Write(GetProjects(Request("cid"), Request("status")))
                    Exit Select
        end select
end if

[ jquery]

$.ajaxSetup({
        error: function(xhr, msg) { alert(xhr, msg) },
        type: "POST",
        url: "Ajax.aspx",
    beforeSend: function() { showLoader(el); },
        data: { method: 'GetProjects', cid: "2", status:"open"},
        success: function(msg) {
            var data = JSON.parse(msg);
            alert(data.Message);
        },
        complete: function() { hideLoader(el);  }
    });

, .

+1

Russ Cam ( , ;)):

jQuery ASP.NET AJAX

+1
source

Dave Ward has a series of posts that use jQuery directly with ASP.Net PageMethods, no MS Ajax UpdatePanel is required. In particular, this post will help you get started.

+1
source

Source: https://habr.com/ru/post/1708883/


All Articles