close
幾年前寫Web Form時有研究成功ajax使用
最近重新建立一個Web Form專案套之前的模式怎麼樣都呼叫不了後端對應的Method,回傳undefined
查詢ajax使用方法對照很久也沒錯啊...浪費兩個小時左右突然遙遠的記憶回來了一點,難不成是有什麼設定要調整?
原來就是這裡!! RouteConfig.cs 裡的 settings.AutoRedirectMode = RedirectMode.Permanent; 註解它就好
關掉這個設定似乎是不會自動導向簡易URL,我是覺得還好啦!
OK~基本設定調整完,後續的使用其實跟google到的沒太大差異
前端使用
var listIndex = $(this).attr("id"); $.ajax({ url: "BarChart.aspx/GetValue", type: "POST", data: JSON.stringify({ index: listIndex }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var list = response.d; alert("Success " + JSON.parse(list['Label']); }, failure: function (response) { alert(response); } })
JSON.parse() 很好用!搭配一些jQuery套件時能從呼叫的Method中回傳串好的Jason格式參數
要注意的是如果像這樣 [[1, 96.68], [1, 97.19], [1, 98.57]]格式的不能單獨回傳[1, 96.68], [1, 97.19], [1, 98.57]再parse會出錯
一定要完整的[[1, 96.68], [1, 97.19], [1, 98.57]]
後端寫法像這樣
[WebMethod(enableSession: true)] public static BarChartValueClass GetValue(string index) { BarChartValueClass data = (BarChartValueClass)HttpContext.Current.Session["BarChartValueClassSession"]; return data; }
[WebMethod] 這個要記得標註,且須為靜態的方法
進階寫法像Get就不多說了!!
文章標籤
全站熱搜