2015-06-08 91 views
0

我有一箇舊的WebForms項目需要重新訪問,我想添加一些新功能。WebForms GET Json請求(JsonRequestBehavior.AllowGet)

我想加載使用$ .getJSON()的數據,這是在ASP.NET MVC很簡單,因爲你可以返回這樣的數據,

return Json(data, JsonRequestBehavior.AllowGet); 

的Get不工作中的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); }); 

這裏是的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

任何幫助表示讚賞。

由於

+0

希望此鏈接將是有用的http://codepedia.info/2015/02/jquery-ajax-json-example-asp-net-sql-database/ –

+0

另外[SO交](HTTP:/ /stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working)顯示如何做到這一點。 – Michael

回答

0

需要添加 「[ScriptMethod(UseHttpGet =真)]」 前method.This將允許GET方法。 例如。

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

您能否爲您的問題添加一些解釋? – honk