$(" #id")。load和$ .ajax之间的区别? [英] Difference between $("#id").load and $.ajax?

查看:124
本文介绍了$(" #id")。load和$ .ajax之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道 $(#id)之间的区别.load $ .ajax

解决方案

让我为你澄清一些事情:



$。ajax()是jQuery提供的基本和低级ajax函数,这意味着你可以做任何你想要的工作 XmlHttpRequest 对象。但曾几何时jQuery Developers认为除了 $ .ajax()之外,他们可以为开发人员提供更具体的方法,这样他们就不需要传递更多参数来制作 $。ajax()方法以他们想要的方式工作。例如,他们说不是将 json 作为参数传递给 $ .ajax()来表示返回数据类型,提供 $。getJSON()所以我们都知道我们预期的返回类型是 json ,或者不是指示发送方法为发布获取,您可以使用 $ .post() $。get()



所以 load() 是一回事,它可以帮助你将html数据注入你的html。用 load()方法你知道html部分是预期的。



这不是很酷吗? / p>

我想我已经坠入爱河。



欲了解更多信息,你可以访问jquery.com,他们甚至提供了新的图书馆和api教程页面。



编辑:

  $ .ajax({
type:POST,
url:some.php,
data:name = John& location =波士顿,
成功:功能(msg){
alert(数据保存:+ msg);
}
});

与以下相同:

  $ .post(some.php,{name:John,time:2pm},
function(data){
alert(Data已加载:+数据;
});

现在您可以看到它是 $。ajax的简化版本(ajax) ),要进行帖子调用,你需要传递一些发送方法类型的信息,如 post ,如第一个例子所示,但不是做你可以使用 $ .post(),因为你知道你在做什么发布所以这个版本更加简化了并且易于使用。



但不要忘记一些事情。除了 load()之外,所有其他ajax方法都返回XHR(XmlHttpRequest实例),因此您可以将它们视为使用XmlHttpRequest,实际上您正在使用它: )但是 load()返回jQuery,这意味着:

  $( #objectID)。load(test.php,{'choices []':[Jon,Susan]}); 

在上面的示例中,您可以轻松地将返回html注入 #objectID 元素。不是很酷吗?如果它没有返回jQuery,你应该一直在使用回调函数,你可能从 data 对象中得到结果并将其手动注入到你想要的html元素中。所以这很麻烦但是使用 $ .load()方法,它在jQuery中真的很简单。

  $(#feeds)。load(feeds.php,{limit:25},function(){
alert(Feed中的最后25个条目已被删除loading);
});

您甚至可以发布参数,因此根据这些参数,您可以在服务器端和将html部分发送回客户端,你可爱的jQuery代码接受它并将其注入上面示例中的 #feeds html元素。


Does anyone know what is the difference between $("#id").load and $.ajax?

解决方案

Let me clarify things for you a little bit :

$.ajax() is the basic and low-level ajax function jQuery provides which means you can do what ever you want to like you can work with XmlHttpRequest object. But once upon a time jQuery Developers thought that actually besides $.ajax(), they could provide more specific methods to developers so they wouldn't need to pass more parameters to make $.ajax() method work the way they want. For example they said instead of passing json as a parameter to $.ajax() to indicate return data type, they provided $.getJSON() so we would all know that the return type we expected was json, or instead of indicating send method as post or get, you could use $.post() or $.get() respectively.

So load() is the same thing, it can help you inject html data into your html. with load() method you know that an html portion is being expected.

Isn't that cool ?

I think I've been fallen in love.

For more information, you can visit jquery.com, they are even providing their new library and api tutorial page.

Edit :

 $.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 }); 

is the same as below :

$.post("some.php", { name: "John", time: "2pm" },
   function(data){
     alert("Data Loaded: " + data);
   });

Now as you can see it is the simplified version of $.ajax(), to make post call, you need to pass some information of send method type which is post as shown at the first example but instead of doing this you can use $.post() because you know what you are doing is post so this version is more simplified and easy to work on.

But don't forget something. Except for load(), all other ajax methods return XHR (XmlHttpRequest instance) so you can treat them as if you were working with XmlHttpRequest, actually you are working with it tho :) and but load() returns jQuery which means :

$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );

in the example above, you can easly inject the return html into #objectID element. Isn't it cool ? If it wasn't returning jQuery, you should have been working with callback function where you probably get the result out of like data object and inject it manually into the html element you want. So it would be hassle but with $.load() method, it is really simplified in jQuery.

$("#feeds").load("feeds.php", {limit: 25}, function(){
   alert("The last 25 entries in the feed have been loaded");
 }); 

You can even post parameters, so according to those parameters you can do some work at server-side and send html portion back to the client and your cute jQuery code takes it and inject it into #feeds html element in the example right above.

这篇关于$(" #id")。load和$ .ajax之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆