Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the return type of ajax request

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge of this article "what is the return type of ajax request?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the return type of ajax request" article.

Ajax request return types are: 1, xml type, available jQuery processing; 2, html type (plain text HTML information); 3, script type (plain text JavaScript code); 4, json type; 5, jsonp type; 6, text type (plain text string).

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Ajax detailed explanation

Ajax = asynchronous JavaScript and XML.

Ajax is a technology for creating fast dynamic web pages.

By exchanging a small amount of data with the server in the background, Ajax can update web pages asynchronously. This means that some part of the page can be updated without reloading the entire page.

Traditional web pages (without Ajax) if you need to update the content, you must reload the entire web page.

(note: the picture is from the Internet)

How to use Ajax Technology

First, you need to get the XMLHttpRequest object:

Var xhr;xhr = new XMLHttpRequest ()

The XMLHttpRequest object has five core properties:

Onreadystatechange: when the state of preparation changes

ReadyState: preparation status. The value of this attribute may be a number between 0x4. 0 indicates that no connection has been established, and 4 indicates that a response has been received.

Status: response code, such as 404,200

ResponseText: string of response

ResponseXML: XML of the response

When you need to make a request, you need the open () and send () methods of the XMLHttpRequest object:

Open (request method, request path, whether asynchronous)

Send ()

Use the demo:

/ / asynchronously check whether the user name exists function checkUsername (username) {/ / get the XMLHttpRequest object var xhr = new XMLHttpRequest (); var url = "check_username.do?username=" + username / / configure onreadystatechange xhr.onreadystatechange = function () {/ / when the server has responded (4) and the response code is 200 if (xhr.readyState = = 4 & & xhr.status = = 200) {/ / based on the server's response Displays the prompt message if (xhr.responseText = = "1") {/ / indicates that the user name exists document.getElementById ("username_hint") [xss_clean] = "user name is correct" } else {/ / indicates that document.getElementById ("username_hint") [xss_clean] = "user name does not exist";}; / / call function xhr.open ("GET", url, true); xhr.send ();}

In JQuery, Ajax can be implemented in three ways:

$.ajax ({"url": ", / / access path" data ":", / / data to be transferred "type": ", / / request method" dataType ":", / / return value type "success": function (obj) {} / / callback function "error" if the response is successful: function (obj) {} / / callback function} if the response fails) $.get (URL,callback); $.post (URL,data,callback)

What are the return value types when using Ajax?

Xml 、 html 、 script 、 JSON 、 jsonp 、 text

Xml: returns the XML document, which can be processed with jQuery.

Html: returns plain text HTML information

Script: returns plain text JavaScript code. The results are not automatically cached. Unless the "cache" parameter is set

Json:json and html are exactly the same in requests and servers. The return values of requests are actually String objects, with two differences. First, there is no limit on the string format returned in html mode, while in json mode, it must comply with the specification of json protocol. Second: sucuess is called back directly without any operation after the completion of the request in html mode, while json adds eval and executes the returned string to see the source code data = eval_r ("(" + data + ")"); the json object is returned; (when the return value of the method is Javabean, the response is in json string format in the response body)

Jsonp:jsonp interacts in the same way as js. XmlHttpRequest objects cannot be accessed across domains, but the src of script tags can be accessed across domains. Here are two concepts: the first Ajax cannot be operated across domains, and the second jQuery's jsonp can be operated across domains. What on earth is jsonp? It is an unofficial definition, and the current specification requires the cooperation of the server and the client.

Text: returns a plain text string.

The above is the content of this article on "what is the return type of ajax request?" I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report