1

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

shammelburg
  • 6,974
  • 7
  • 26
  • 34
  • Another [SO post](http://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working) that shows how to do it. – Michael Jun 08 '15 at 08:39

1 Answers1

1

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;
}
Naveen Sharma
  • 67
  • 1
  • 5