$(function () { var ajax = function (options) { options = $.extend({ test: false, div: undefined, anonymous: false, title: '未录入', url: '', method: 'get', data: '', headers: {}, success: function (result) { }, error: function (textStatus, errorThrown) { } }, options); if (options.url == '') { document.writeln("请求地址丢失!"); return; } $.ajax({ url: options.url, data: options.data, type: options.method, headers: options.headers, success: function (result) { if (!options.test) { var $body = $('#main-content'); $body.append('
'); var $section = $body.find('section').last(); $section.append($.format('

{0}

', options.title)); $section.append($.format('
{0}
', options.url)); $section.append($.format('
{0}
', options.anonymous ? "允许" : "不允许")); $section.append($.format('
{0}
', JSON.stringify(options.headers))); $section.append($.format('
{0}
', options.method)); $section.append($.format('
{0}
', JSON.stringify(options.data))); $section.append($.format('
{0}
', JSON.stringify(result))); $section.append('
'); $section.append($.format('
', JSON.stringify(options), JSON.stringify(options.data))); $section.append('
') options.success(result); } else { if (options.div) { options.div.html($.format('
{0}
', JSON.stringify(result))).show(); } } }, error: function (XMLHttpRequest, textStatus, errorThrown) { var $body = $('body'); var $section = $body.find('section').last(); $section.append($.format('
textStatus:{0}
', textStatus)); $section.append($.format('
errorThrown:{0}
', errorThrown)); options.error(textStatus, errorThrown); } }); }; $('body').on('click', 'button', function () { var $input = $(this).parent().prev(); var options = $.extend(JSON.parse($input.attr('data-val')), { data: JSON.parse($input.val()) }, { test: true, div: $(this).parent().parent().next() }); ajax(options); }); // 接口调用 ajax({ title: '用户登陆调用接口', anonymous: true, url: '../api/login', method: 'POST', data: { userName: 'Test', password: '1' }, success: function (result) { var token = result.Token; ajax({ title: '用户登陆信息接口', url: '../api/login', headers: { Token: token }, method: 'GET' }); ajax({ title: '指定用户信息接口', url: '../api/Users', headers: { Token: token }, method: 'GET', data: { userName: "Test" } }); ajax({ title: '检查当前用户名是否可用接口', url: '../api/Users', headers: { Token: token }, method: 'PUT', data: { UserName: "101", UserStatus: 9 } }); ajax({ title: '更改用户显示名称接口', url: '../api/Users', headers: { Token: token }, method: 'PUT', data: { UserName: "101", UserStatus: 1, DisplayName: "1010" } }); ajax({ title: '更改用户密码接口', url: '../api/Users', headers: { Token: token }, method: 'PUT', data: { UserName: "101", UserStatus: 2, Password: "1", NewPassword: "2" } }); ajax({ title: '新建用户接口', url: '../api/Users', headers: { Token: token }, method: 'POST', data: { ID: 0, UserName: "102", Password: "1", DisplayName: "102" } }); ajax({ title: '更新用户接口', url: '../api/Users', headers: { Token: token }, method: 'POST', data: { ID: 21, UserName: "102", Password: "1", DisplayName: "102" } }); ajax({ title: '删除用户接口', url: '../api/Users', headers: { Token: token }, method: 'DELETE', data: { "": "50,51" } }); } }); });