WebForms GET Json请求(JsonRequestBehavior.AllowGet)

时间:2015-06-08 08:28:11

标签: javascript c# jquery asp.net ajax

我有一个需要重新访问的旧WebForms项目,我想添加一些新功能。

我想使用$ .getJSON()加载数据,这在ASP.NET MVC中非常简单,因为你可以像这样返回数据,

return Json(data, JsonRequestBehavior.AllowGet);

Get在WebForms中不起作用,

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

邮报工作。

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

这是WebForms WebMethod,

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

我想使用GET方法返回json。

WebForms是否有JsonRequestBehavior.AllowGet

感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

需要在方法之前添加“[ScriptMethod(UseHttpGet = true)]”。这将允许GET方法。 例如

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