jQuery ajax()需要URL吗?

4
ajax函数的头部是:jQuery.ajax( url [, settings ] ) 文档中说URL是必需的,为什么示例中只提供了“settings”?
$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script"
});

4
阅读整个页面。 - zzzzBov
4个回答

7

7

接下来,您将看到与 $.ajax 相关的另一种语法,其中只传递了一个包括URL在内的设置对象。

此外,您可以在这里查看其他语法


4

如果您调用当前页面,则不需要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.

0

我真的只看到一个例子没有使用URL(使用statusCode设置的那个):

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

看起来他们故意省略了URL,以便在未找到指定的操作时显示(因为没有指定URL)。您需要指定URL,因为无论您使用AJAX还是同步旧方式进行服务器请求,都需要告诉浏览器将请求发送给谁。我在jQuery文档页面中看到的几乎所有示例都有指定的URL或某种形式(url:“test.html”,url:a_cross_domain_url,url:“http://fiddle.jshell.net/favicon.png”)。查看所有文档示例始终很有用,以便更好地了解语法及其功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接