JQuery ajax call to httpget webmethod (c#) not working

Posted by Tim Jarvis on Stack Overflow See other posts from Stack Overflow or by Tim Jarvis
Published on 2010-04-16T06:38:27Z Indexed on 2010/04/16 6:43 UTC
Read the original article Hit count: 769

Filed under:
|
|
|

I am trying to get an ajax get to a webmethod in code behind. The problem is I keep getting the error "parserror" from the JQuery onfail method.

If I change the GET to a POST everything works fine. Please see my code below.

Ajax Call

<script type="text/javascript">
        var id = "li1234";

        function AjaxGet() {
            $.ajax({
                type: "GET",
                url: "webmethods.aspx/AjaxGet",
                data: "{ 'id' : '" + id + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(msg) {
                    alert("success");

                },
                error: function(msg, text) {
                    alert(text);
                }
            });
        }

    </script>

Code Behind

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true,
    ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] 
public static string AjaxGet(string id)
{
    return id;
}

Web.config

        <webServices>
            <protocols>
                <add name="HttpGet"/>
            </protocols>
        </webServices>

The URL being used

......../webmethods.aspx/AjaxGet?{%20%27id%27%20:%20%27li1234%27}

As part of the response it is returning the html for the page webmethods.

Any help will be greatly appreciated.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about webmethods