What should I use? AJAX or jQuery?
I have 3 ASP list boxes. The list of values that appear in the second list box depend on the selected item in the first and the values that appear in the thir开发者_如何学编程d box appear based on the selection in the second box. All these values are to be retrieved from the database and I would like to know if its better to use AJAX or jQuery.
I have read up the differences between the two, but am still confused. Please help me with this particular example.
Ajax means "Sending data to and/or receiving data from the server without leaving the page using JavaScript"
jQuery is a JavaScript library.
You can't choose between them, since that is like choosing between "Travelling" and "Boeing" (Boeing make products that help you travel. Other companies do too. You can travel without using products made by any company. Boeing make non-travel related things).
If you have a lot of data (i.e. too much to load everything into the page up front) then you will want to use Ajax to fetch it. If you don't have that much data, you can just embed it in your script and not worry about making HTTP requests to get it using Ajax techniques (this will have a cost at load time, but will make the script rather simpler).
If you want to change data in a select element based on data selected in a previous one, then a JavaScript library could save you some hassle, but might not be worth the additional page weight (although if you do many things with JS then the effort will add up until it would have been worth using a library from the outset).
If you want to use Ajax, then there are sufficient browser differences that a JS library would probably be worth the weight. As libraries go, jQuery is reasonable and popular.
Feathers or lead?
It's not either-or! AJAX is a technique for talking to a web server via Javascript. jQuery is a Javascript framework. They're apples and oranges. You can do AJAX requests in plain Javascript, or you can use the jQuery wrappers to make AJAX requests. In the end they're the same, jQuery just makes it easier because it abstracts things. jQuery is not an alternative to AJAX or Javascript, it's just a collection of functions that make everyday tasks easier.
I'd recommend you start with plain Javascript AJAX requests to learn how things work before you use a library to abstract the details.
They are different (albeit related) concepts. jQuery is a rich JavaScript library that provides, among many other things, AJAX support.
AJAX itself is mostly a generic term for various JavaScript techniques used to connect to a web server dynamically without necessarily loading multiple pages. In a more narrowly-defined sense, it refers to the use of XmlHttpRequest object to interact with a web server dynamically via JavaScript.
jQuery is a JavaScript library aimed at eliminating differences between user agents.
Ajax (Ansynchronous JavaScript and XML) is a technique to perform asynchronous requests to the host. jQuery (among many other libraries … and vanilla JavaScript) supports Ajax (and makes it easier by providing a standardized interface across user agents).
So, the real question is: should you use jQuery to perform your Ajax requests?
And the answer is: yes, if you're already using jQuery - if not, you can include jQuery or another Ajax-supporting JS library, or you can implement the Ajax functionality in vanilla JS.
are you not getting confused between AJAX the technique and jQuery the library... imo use jQuery to perform AJAX
i would personally use jquery with getJson to retreive the data. Maybe this is an option for you?
$.getJSON("getMenu", "parentID=" + parent, function (data) {
$.each(data, function (index, item) {
// add code to new 2nd listbox
}
}
then you would have to declare getMenu backend
this can ofc also done with ajax
Ajax is a method that describes asynchronious request that are (generally) called by some Javascript code. jQuery is a library that implements this technique in some pretty nifty wrappers. I suggest that you download jQuery, it is quite easy and there is lots of code and tutorials on the internet.
if you appear data is too much! then you can use Ajax,Because of the speed
if not too much you can use javascript(jQuery) to read them once!
are you using ASP or ASP.NET? If it is ASP.NET, then simply add code to the onselectionchanged method of the list box to change the values in the other boxes. ASP.NET will take care of all the AJAX calls.
What exactly do you mean when you say "AJAX"? As far as I can see no matter what library you use (jQuery or something else) you will still use Asynchronous server requests.
精彩评论