开发者

webservice doesn't get called by jQuery

开发者 https://www.devze.com 2022-12-31 18:35 出处:网络
I want to call a webservice using jQuery.ajax() but the webervice doesn\'t get called. - If I change the url: to reference a .ashx file it gets called but not .asmx?

I want to call a webservice using jQuery.ajax() but the webervice doesn't get called. - If I change the url: to reference a .ashx file it gets called but not .asmx?

Here's the code I'm using:

jQuery.ajax({
        type: "POST",
        url: "/services/CheckUsername.asmx/CheckUsername", // this doesn't get called
        //url: "/services/CheckUsername.ashx/ProcessRequest", this gets called
        data: '{ "con开发者_C百科text": "' + "username" + '"}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',

        success: function (msg) {
            alert("Result: " + msg);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Error: " + textStatus)
        }

The .ashx file gets called but a parsererror is retuned because it's returning http context - how can I modify this to get a string return type from a webservice?

Thanks,


try setting [ScriptMethod] attribute to your server side method


I found out how to pass the username entered by the user to the HTTP handler. Here's the jQuery code:

jQuery.validator.addMethod("UsernameCheck", function (value, element) {
        var allowed = true;
        jQuery.ajax({
            url: "/services/UsernameCheck.ashx",
            global: false,
            cache: false,
            type: "get",
            data: "profile_name=" + jQuery("#username").val(),
            dataType: "text",
            success: function (msg) {
                if (msg == 1) {
                    allowed = true;
                }
                else {
                    allowed = false;
                }
            }
        });
        return allowed;
    }, "Please enter a valid username");
0

精彩评论

暂无评论...
验证码 换一张
取 消