Issues calling a page method from javascript

35 views Asked by At

I haven't used this before so I'm probably missing something, is this even possible?

 function myFunc(obj) {
        $(document).ready(function () {

            $.ajax({
                type: "POST",
                url: "CMSWebParts_PSS_Test.aspx/CallMe",
                data: obj,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                }
            });
        });
    }

And the .cs:

    public partial class CMSWebParts_PSS_Test : CMSAbstractWebPart
    { ...
    [WebMethod]
        public static string CallMe(string data)
        {
           return card;
        }...
    }

I keep getting 404. Tried using PageMethods but still getting the same result.

1

There are 1 answers

2
wazz On

Make your data a string

data: JSON.stringify(obj),

msg is a wrapper. The requested data is put into a property -> d

success: function (msg) {
    alert(msg.d):
}

Also, call your function myFunc or use $(document).ready.


Update: try moving the success part outside of the initial call, change the name to 'done'.

$.ajax({
    type: "POST",
    url: "CMSWebParts_PSS_Test.aspx/CallMe",
    data: JSON.stringify(obj),
    contentType: "application/json; charset=utf-8",
    dataType: "json"        
}).done(function (msg) {
    alert(msg.d);
)}.fail(function() {
   //
});