🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## $.ajax 语法 ``` jQuery.ajax( url [, settings ] ) 参数说明: settings 键值对组成的对象 accepts 内容类型发送请求头(Content-Type) async 默认(true),设置为fasle,则为同步请求 beforeSend 请求发送前的回调函数,函数返回 fasle,则取消这个请求 contentType 默认为: 'application/x-www-form-urlencoded; charset=UTF-8' complete 完成请求后调用 context complete回调函数的上下文为这个DOM元素如:"document.body" data 参数对象 dataType xml, json, script, or html error 请求失败时候调用 global 默认(true),设置为false,则不会触发全局 AJAX 事件 headers 设置头对象 jsonp jsonpCallback statusCode 如设置为 statusCode: { 404: function() { alert("page not found"); } 则会触发此函数 success 成功时调用 timeout 设置超时时间为毫秒 type 默认为GET ``` ### 替换为新的接口 ``` jqXHR.done(function(data, textStatus, jqXHR) {}); // 替换success jqXHR.fail(function(jqXHR, textStatus, errorThrown) {}); // 替换 fail jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { }); // 替换 complete ``` 示例 ``` var jqxhr = $.ajax( "example.php" ) .done(function() { alert("success"); }) .fail(function() { alert("error"); }) .always(function() { alert("complete"); }); // perform other work here ... // Set another completion function for the request above jqxhr.always(function() { alert("second complete"); }); ``` ## 示例 ### post ``` $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }).done(function( msg ) { alert( "Data Saved: " + msg ); }).fail(function(jqXHR, textStatus) { alert( "Request failed: " + textStatus ); }); ```