简体   繁体   中英

using Get for ajax calls gives me 500 error

I was calling a [web method] using POST but as I am 'getting' data back I am trying to use GET instead.

Using Post works. Using GET gives me a 500 error.

This is the main jquery call to my [web method]:

        $.ajax({
            type: 'GET',
            contentType: 'application/json',
            dataType: 'json',
            url: 'Cloud/Feed.aspx/GetNextFrames2',
            data: '{ test: "hime"}',

~

This is my test [web method].

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string GetNextFrames2(string test)
{
    return 'test'
}

If I do not pass any parameters I will get no error using GET. As soon as add a parameter I get 500 internal error.

I have used wireshark and Fiddler but I cannot see anything useful.

This is obviously down to using parameters. So, at least I have pinpointed where the error is.

I have tried passing the parameters directly appended to the url:

myurl?par=testme...

but still same error.

What else can I try?

Thanks

The problem is with the value in the query string. The value should be within quote. The below code is working.

$.ajax({
            type: 'GET',
            contentType:'application/json',
            dataType: 'json',
            cache:false,
            url: "TestWebMathod.aspx/GetNextFrames2?test='hime'",
            error: function (error) {
                alert(error.responseText)
            },
            success: function (result) {
                alert(result.d)

            }

        });

Also look at the error.responseText to know the exact exception message. This will help to resolve the error.

Ajax GET requests shouldn't have Content-type since they don't have any entity-body.

For reference : Do I need a content type for http get requests?

Try

$.ajax({
        type: 'GET',
        dataType: 'JSON',
        url: "Cloud/Feed.aspx/GetNextFrames2?test='hime'",

either of which should work

The comments above about contentType for GET are correct BUT WebMethods require it - see this detailed post on why .

So really, unless you do POST , IMHO, it's just a pain to get things to work with WebMethods . I can't even get it to work with GET if the method has a param (it'll bomb out), so if someone can get it to work, bravo! Note that even the link example using GET includes a param...unsure if that's simply something that used to be true (the date on that article is 2007 ).

So if you must ( updated based on answer above ):

    $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "Cloud/Feed.aspx/GetNextFrames2",
            //data: {"test": "foo"}, // GET -> Cloud/Feed.aspx/GetNextFrames2?test=foo
            data: {"test": "'foo'"}, //value now in quotes
            ...

WebMethod:

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string GetNextFrames2(string test)
    {
        //Should now work based on answer above

        //Yes, this is FUBAR, but technically feasible (no need for params in method)
        //var query = HttpContext.Current.Request.QueryString;

        //now do what you need to do with your querystring data 
        // re: that's where your data is in GET ($.get)

        ....           
    }

Other awe-inspiring notes:

  • if you have FriendlyUrls , you will have to comment out AutoRedirectMode = RedirectMode.Permanent in RouteConfig -> FriendlyUrlSettings - because it does what it says and redirect .aspx to a "friendly url"...yup including above...

Hope this helps and maybe get you thinking (instead) of WebAPI :)


Update Summary

Well, the answer as shown above is to wrap string in quotes - so it seems to (strongly) type the parameters. So if int :

  • if $.get data: { "test": 55 }

     [WebMethod] [ScriptMethod(UseHttpGet = true)] public static string GetNextFrames2(int test) { ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM