JS Ajax compatible Multi-browser get post solution garbled response process all __JS

Source: Internet
Author: User
Tags browser cache
	Resolves a JSON object to a string and replaces the corresponding symbol function jsonstringify (obj) {return json.stringify (obj). Replace (/"|{|}
	/g, ""). Replace (/:/g, "="). Replace (/,/g, "&");
		function Getserverdatauseajax (method, URL, params) {//Start initializing XMLHttpRequest object var http_request = false; if (window.
			XMLHttpRequest) {//mozilla browser http_request = new XMLHttpRequest ();
			if (http_request.overridemimetype) {//Set MIME category Http_request.overridemimetype (' Text/xml '); ' Else if ' window.
			ActiveXObject) {//IE browser try {http_request = new ActiveXObject ("Msxml2.xmlhttp");
				catch (e) {try {http_request = new ActiveXObject ("Microsoft.XMLHTTP");
			catch (e) {}} if (!http_request) {//exception, create object instance failed Window.alert ("Cannot create XMLHttpRequest object instance.");
		return false;
			///Different request method request process different//development process 80% development Use GET method request if (method.tolowercase () = = "Get") {//here plus timestamp is to solve the problem of low version IE browser cache
	Another: Here is an extra method for parsing the incoming JSON object and string recombination URL = "?" + jsonstringify (params) + "Xtamp"				+ New Date (). GetTime (); If not IE browser execute//if (Navigator.userAgent.indexOf ("MSIE") <=0) {//Netscape.security.Privilege
           Manager.enableprivilege ("Universalbrowserread");

			//Note This is two times encodeURI () Http_request.open (method, encodeURI (encodeURI (URL)), true); Functions that process return information Http_request.onreadystatechange = function () {if (http_request.readystate!= 4) {//Judge object state//i 
					D loading_inserhtml//document.getElementById (' Merchname '). innerhtml= "Loading data ...";
				Alert (4); else {if (Http_request.status = | | http_request.status = 0) {//information has been successfully returned, start processing information var strhtml = Http_re
						Quest.responsetext;
						ok_innerhtml//document.getelementbyid (' Merchname '). innerhtml=strhtml;
					alert (strhtml); else {//page is not normal alert ("the page you are requesting has an exception.)
					");
			}
				}
			};
		Start sending request http_request.send (NULL); else {if (method.tolowercase () = "POST") {if (method.tolowercase () = "post") {HTTP_request.open (method, URL, true);
					Http_request.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
							Functions to process return information Http_request.onreadystatechange = function () {if (http_request.readystate!= 4) {//Judge object state 
							document.getElementById (' Merchname '). innerhtml= "Loading data ...";
						Alert (4); else {if (Http_request.status = | | http_request.status = 0) {//information has been successfully returned, start processing information var strhtml = htt
								P_request.responsetext;
								document.getElementById (' Merchname '). innerhtml=strhtml;
							alert (strhtml); else {//page is not normal alert ("the page you are requesting has an exception.)
							");
					}
						}
					};
				Start sending Request Http_request.send (Jsonstringify (params));
					else {//http Request category parameter Error Window.alert ("HTTP request class parameter error ...");
				return false; 
			function test () {//{name: ' Jack ', age:88}/Note The parameter object given here is the JSON Object format getserverdatauseajax ("Get", "test", {
	Name: "Heaven and Earth", age:88}); }
server-side decoding Urldecoder.decode (Request.getparameter ("name"), "UTF-8") is required for use
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.