jQuery.ajaxSetup()| jqueryAPI 2.2 中文手册- AspRain.cn 致力于Web开发技术翻译整理

jQuery API 2.2.0

jQuery.ajaxSetup()

分类:Ajax > 底层接口

返回:

jQuery.ajaxSetup( options )

描述:为未来的Ajax请求设置默认值。不建议使用它。

加入于: 1.1
jQuery.ajaxSetup( options )
  • options
    类型:PlainObject
    A set of key/value pairs that configure the default Ajax request. All options are optional.

For details on the settings available for $.ajaxSetup(), see $.ajax().

All subsequent Ajax calls using any function will use the new settings, unless overridden by the individual calls, until the next invocation of $.ajaxSetup().

注意: The settings specified here will affect all calls to $.ajax or Ajax-based derivatives such as $.get(). This can cause undesirable behavior since other callers (for example, plugins) may be expecting the normal default settings. For that reason we strongly recommend against using this API. Instead, set the options explicitly in the call or define a simple plugin to do so.

For example, the following sets a default for the url parameter before pinging the server repeatedly:

$.ajaxSetup({
  url: "ping.php"
});

现在每次制作了一个Ajax请求时,会自动使用“ping.php”这个URL:

$.ajax({
  // 这里没有设置url,就使用ping.php
  data: { "name": "Dan" }
});

Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart().ajaxStop().ajaxComplete().ajaxError().ajaxSuccess().ajaxSend()—rather than within the options object for $.ajaxSetup().

示例

Sets the defaults for Ajax requests to the url "/xmlhttp/", disables global handlers and uses POST instead of GET. The following Ajax requests then sends some data without having to set anything else.

$.ajaxSetup({
  url: "/xmlhttp/",
  global: false,
  type: "POST"
});
$.ajax({ data: myData });

如果网页上不能运行示例,请点击http://www.asprain.cn/jQueryAPI/jquery.ajaxsetup.htm查看示例。

如果你觉得本文档对你有用,欢迎给翻译作者支付宝打赏,支持翻译作者源源不断翻译更多有用的技术文档。