4

ajax 函数的标题是:jQuery.ajax( url [, settings ] )

文档说 URL 是必需的,为什么在示例中只提供“设置”?

$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script"
});
4

4 回答 4

7

在其下方,您将看到 的其他语法$.ajax其中仅传递了一个设置对象,包括 URL。

于 2013-05-09T19:24:41.127 回答
7

它们提供了两个方法签名

jQuery.ajax( url [, settings ] )

jQuery.ajax( [settings ] )
于 2013-05-09T19:24:48.857 回答
3

如果您调用当前页面,则不需要 URL。

来自:http ://www.sitepoint.com/use-jquerys-ajax-function/

(...) In the second form, the URL is specified in the options parameter, 
or can be omitted in which case the request is made to the current page.
于 2016-03-30T09:19:38.950 回答
0

我真的只看到了一个不使用 url 的示例(使用 statusCode 设置的示例):

$.ajax({
  statusCode: {
   404: function() {
    alert("page not found");
   }
  }
});

我似乎他们故意省略了 url 以向您显示在 url 不可避免地找不到之后指定的操作(因为没有指定 url)。您需要指定 url,因为每当您发出服务器请求(无论是使用 AJAX 还是同步旧方式)时,您都需要告诉浏览器将请求发送给谁。我在jQuery 文档页面中看到的几乎所有示例都有指定的 URL 或某种类型(url:“test.html”,url:a_cross_domain_url,url:“ http://fiddle.jshell.net/favicon.png ”)。查看所有文档示例以更好地了解语法及其作用总是很有用的。

于 2013-05-09T19:32:46.640 回答