WebForms GET Json requests (JsonRequestBehavior.AllowGet)

1.2k views Asked by At

I have an old WebForms project that needs revisiting and I want to add some new features.

I want to load the data using $.getJSON(), this is very simple in ASP.NET MVC because you can return the data like this,

return Json(data, JsonRequestBehavior.AllowGet);

The Get doesn't work in WebForms,

// Http GET request
$.getJSON('Attributes.aspx/GetAttributes', { 'ProductId': '3' }, function (d) { alert(d); });

The Post works.

// Http POST 
$.post('Attributes.aspx/GetAttributes', { 'ProductId': '3' }, function (d) { alert(d); });

Here is the WebForms WebMethod,

[WebMethod]
public static string GetAttributes(string ProductId)
{
    List<QuoteAttribute> list = DAL.GetAttributes(ProductId);
    var json = JsonConvert.SerializeObject(list);
    return json;
}

I want to return json using the GET method.

Is there an JsonRequestBehavior.AllowGet for WebForms?

Any help is appreciated.

Thanks

1

There are 1 answers

1
Naveen Sharma On

Need to add "[ScriptMethod(UseHttpGet = true)]" before the method.This will allow GET method. eg.

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetAttributes(string ProductId)
{
    List<QuoteAttribute> list = DAL.GetAttributes(ProductId);
    var json = JsonConvert.SerializeObject(list);
    return json;
}