Javascript--ajax notes (to be sorted)

Source: Internet
Author: User
Tags subdomain

JavaScript Ajax

Request.open ("POST", "get.php", true);
Request.setrequestheader ("Content-type", "application/x-www-form-urlencoded");//Tell the server to be a "POST" request
Request.send ("Username= Wang two Dog &password=123456");

HTTP requests have two ways, "post" and "get", "post" is generally used to modify server data when using such as submit form, security, "get" is generally used to request data, the obtained data will be displayed on the URL


var request = new XMLHttpRequest (); Create an asynchronous object, set up an XMLHttpRequest object, IE5, 6 not support, through this object as the foreground and background of the intermediate processor, data interaction

Request.open ("GET", "get.php?") number=1&sex= male ", true);//" GET "defines the way and URL and parameters that are sent after the" post "parameter is written in the send (parameter);
Request.send ();//Send the HTTP request required by the foreground

Request.onreadystatechange = function () {//onreadystatechange Listen for request
if (request.readystate===4&&request.status===200) {//request.readystate Judgment response Change-4 means response complete
Request.status to determine if the request completed--200 complete
var text = Request.responsetext; Get query Results
}
}

Server-side (URL address) need to write the interface to handle the request sent by the client and do the corresponding action

The browser enters the URL in the Address bar and the carriage return belongs to the Get method, the server side receives this HTTP request, in the written script to judge, matches the return page

(The process of returning an HTML page when the browser accesses the server via a URL and the server evaluates to True?) )

Hit URL in the address bar to send parameters: URL? number=1&sex= Male


When the server returns JSON data, the client uses var jsondate = Json.parse (request.responsetext)//To get the JSON-formatted data object

JQuery Ajax
$ (function () {
$.ajax () {

Type: "POST",//"GET" does not require data
Url:
data:{
Name:$ (). Val (),
Number:$ (). Val (),
Sex:$ (). Val (),
Job:$ (). Val ()
},
DataType: "JSON",
Success:function (obj) {

},
Error:function (obj) {
Alert ("Error occurred:" +obj.status);
}
}

})

Cross-domain

http://www. Abc.com:8080/scripts/jquery.js
Protocol subdomain primary Domain name Port number Request resource address


When the protocol subdomain primary domain port number is not the same, the different domains, the mutual request resources between different domains count as "cross-domain"


Solution:

1, backstage use agent

2, Jsonp
Front DataType: "Jsonp",
JSONP: "Callback",

Background $JSOPN = $
$jsopn.

3, XHR2

Javascript--ajax notes (to be sorted)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.