Home > Web Front-end > JS Tutorial > How to use the ajax.load() method in jQuery

How to use the ajax.load() method in jQuery

php中世界最好的语言
Release: 2018-04-25 16:02:16
Original
1771 people have browsed it

This time I will show you how to use the ajax.load() method in jQuery. What are the precautions when using the ajax.load() method in jQuery. The following is a practical case. Let’s take a look. one time.

jQuery load() method

jQuery load() method is a simple but powerful AJAX method.

The load() method loads data from the server and puts the returned data into the selected element.

Syntax:

$(selector).load(URL,data,callback);
Copy after login

The load() function is used to load data from the server and replace the content of the current matching element with the returned html content .

The load() function uses the GET method by default. If data in object form is provided, it will automatically switch to the POST method.

Because the Get request method is used by default, we can also add data to the url for submission.

For example$("#box").load("loadTest.html?name=zhang&age=25")

load() method can be parameterized Count three parameters:

url (required, the url address of the requested html file, the parameter type is String)

data (optional, the key/value data sent, the parameter type Is Object)

callback (optional, successful or failed callback function, parameter type is Function)

## The #load() method is a local method because it requires a jQuery object containing the element as a prefix. For example, $("#box").load()

and $.get() and $.post() are global methods and do not need to specify an element. For purposes, .load() is suitable for asynchronous acquisition of

static files,

and for those that need to pass parameters to the server page, $.get() and $.post() More appropriate.

The optional callback parameter specifies the callback function to be allowed when the load() method completes. The callback function can set different parameters:

  • responseTxt - Contains the result content when the call is successful

  • statusTXT - Contains the status of the call

  • xhr - Contains the XMLHttpRequest object

The following example will display a prompt box after the load() method is completed. If the load() method is successful, "External content loaded successfully!" will be displayed. If it fails, an error message will be displayed:

 $("button").click(function(){
 $("#p1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
  if(statusTxt=="success")
   alert("外部内容加载成功!");
  if(statusTxt=="error")
   alert("Error: "+xhr.status+": "+xhr.statusText);
 });
});
Copy after login
I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting information. Other related articles on php Chinese website!


Recommended reading:

How to use json as a parameter in js

ajax php control function call Detailed explanation of steps

The above is the detailed content of How to use the ajax.load() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template