JS Ajax No Refresh load page program code

Source: Internet
Author: User
Tags bind json

JS Original Ecological Writing

The code is as follows Copy Code

var mm;
var nn;
function MakeRequest (url,obj,b) {
Mm=obj;
Nn=b;
Http_request = false;
if (window. XMLHttpRequest) {
Http_request = new XMLHttpRequest ();
if (Http_request.overridemimetype) {
Http_request.overridemimetype (' Text/xml ');
}
else if (window. ActiveXObject) {
try{
Http_request = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try {
Http_request = new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {
}
}
}
if (!http_request) {
Alert ("Your browser does not support the current operation, please use IE 5.0 version!");
return false;
}


The method that defines the page invocation init, not init ();
Http_request.onreadystatechange = init;
Http_request.open (' Get ', url, true);

Prohibit IE caching
Http_request.setrequestheader ("If-modified-since", "0");

Send data
Http_request.send (NULL);

Refresh page every 60 seconds
settimeout ("MakeRequest" (' +url+ ")", 1000);
}

function init () {


if (http_request.readystate = = 4) {
if (http_request.status = 0 | | http_request.status = = 200) {
var result = Http_request.responsetext;
if (result== "") {
result = "Get Failed";
}

var z=result.split ("#");

if (z[0]== "BNM")
{
Alert (z[1]);
Window.location.reload ();
}
else if (z[0]== "NM")
{

}
Else
{
document.getElementById ("Pinpai" +mm). Value=z[0];
document.getElementById ("pp" +mm). Value=z[1];
document.getElementById ("LSJ" +mm). Value=z[2];
document.getElementById ("Lxk" +mm). Value=z[3];
document.getElementById ("lb" +mm). Innerhtml=z[4];
}
else {//http_request.status!= 200
Alert ("Request failed!");
}
}
}


jquery+ajax/"target=" _blank ">jquery Ajax

use the Load method for Asynchrony, please.

The code is as follows Copy Code

$ (". Ajax.load"). Load ("/xxx.php",
function (ResponseText, textstatus, XMLHttpRequest) {
this;//here. This points to the current DOM object, which is
$ (". Ajax.load") [0]
alert (responsetext);//Request returned content
alert (textstatus);//Request Status: Success,error
alert (XMLHttpRequest);//xmlhttprequest Object
});


use post to make asynchronous requests


Ajax.aspx:

The code is as follows Copy Code

Response.ContentType = "Application/json";
Response.Write ("{result: '" + request["Name"] + ", Hello! (This message comes from server) '} '); JQuery code:
$.post ("Ajax.aspx", {Action: "Post", Name: "Lulu"},
function (data, textstatus) {
Data can be xmldoc, jsonobj, HTML, text, and so on.
This This AJAX request option configuration information, please refer to Jquery.get ().
alert (Data.result);
}, "JSON");

JQuery Ajax Events

A local event is defined within a method at each AJAX request, for example:

The code is as follows Copy Code
$.ajax ({
Beforesend:function () {
Handle the Beforesend Event
},
Complete:function () {
Handle the Complete event
}
// ...
});

Global events are triggered every time an AJAX request is made, and it broadcasts to all elements in the DOM, and the script loaded in the Getscript () example above is a global Ajax event. Global events can be defined as follows:

The code is as follows Copy Code
$ ("#loading"). Bind ("Ajaxsend", function () {
$ (this). Show ();
}. Bind ("Ajaxcomplete", function () {
$ (this). Hide ();
});

Or:

  code is as follows copy code

 $ ("# Loading "). Ajaxstart (function () {
$ (this). Show ();
}); We can disable global events at a specific request, as long as the global option is set:

 $. Ajax ({
URL: "test.html",
global:false,//disables global Ajax events.)
//...
});

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.