What is the difference between XMLHttpRequest, jQuery.ajax, jQuery.post, jQuery.get

后端 未结 6 1866
无人及你
无人及你 2020-11-29 16:11

How can I find out which method is best for a situation? Can anybody provide some examples to know the difference in terms of functionality and performance?

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 16:35

    jQuery.post and jQuery.get simulate typical page loads, which is to say, you click on a submit button and it takes you to a new page (or reloads the same page). post and get differ slightly in the manner in which the data is sent to the server (good article about it can be found here.

    jQuery.ajax and XMLHttpRequest are page loads similar to post and get, except that the page doesn't change. Whatever information the server returns can be used by javascript locally to be used in any way, including modifying the page layout. They're normally used to do asynchronous work while the user can still navigate the page. Good example of this would be autocomplete capabilities by dynamically loading from a database values to complete a text field. The fundamental difference between jQuery.ajax and XMLHttpRequest is that jQuery.ajax uses XMLHttpRequest to achieve the same effect but with a simpler interface. If you use jQuery I'd encourage you to stick with jQuery.ajax.

提交回复
热议问题