基於JSON的方便性 先設定WebAPI一定是回傳JSON格式
首先要再 Global.asax 檔案的 Application_Start() 事件裡加上
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();在html網頁裡面jQquery使用ajax呼叫 :1: <script>
2:
3: $(document).ready(function () {
4: var json = JSON.stringify({
5: UUID: "F1AC71F4-B9A8-4BFD-941F-68E21689D263",6: Name: "UserName",7: Password: "password",8: Event: "Evt0001"9: });
10: $.ajax({
11: url: "http://localhost:1856/api/TestApI",12: cache: false,13: type: 'POST',14: contentType: 'application/json; charset=utf-8',15: data: json,
16: statusCode: {
17: 201 /*Created*/: function (data) {18:
19: }
20: }
21: });
22: });
23: </script>
當然你得在MVC專案的Controller裡面新增一個TestApiController 因為動作行為是Post所以就寫在Post的Functiob裡面1: public Dictionary<string, string> Post(Dictionary<string, object> value)2: {
3:
4: return new Dictionary<string, string>()5: {
6: {"UUID", value["UUID"].ToString()},7: {"Name", value["Name"].ToString()},8: {"Event", value["Event"].ToString()},9: {"Code", "0"},10: {"Msg", ""},11: {"Link", "http://AD.TEST.com"}12: };
13: }
如此接上去了.
全站熱搜
留言列表