Ajax garbled solution New method _ajax related

Source: Internet
Author: User
Tags send cookies setcookie
Ajax friends must know that JavaScript is using UTF-8 International code, that is, each Chinese character is stored in 3 bytes, but this causes the use of Ajax to send data when garbled.
One solution is to use encodeURIComponent Plus modify content-type for application/x-www-form-urlencoded "to encode the data in a URL format, but there is a disadvantage The UrlDecode using PHP simply can't get the correct text.
Another approach is to convert the data into gb2312 format by using VBScript's written function, which I personally find to be a better approach. Interested friends can go to the Internet to check.
Today's whim, the AJAX call will not send cookies? Write a program immediately to test, sure enough, so that you can invoke Ajax before invoking the data through JavaScript in the cookie, and then send the cookie can be sent out of the data, too cool!!

Demo Address: http://cn5.cn/ajax/ajax12.htm

Client code ajax.htm
Copy Code code as follows:

<title>ajax use cookies to pass the value example </title>
<script>
var odiv
var xh
function GetXML ()
{
Setcookie ($ (' name '). value,$ (' Val '). Value);
Odiv = DOCUMENT.ALL.M
odiv.innerhtml = "Loading, please wait ..."
Odiv.style.display= ""
XH = new ActiveXObject ("Microsoft.XMLHTTP")
Xh.onreadystatechange = Getready
Xh.open ("POST", "a.php", false)
Xh.send ();
}

function Getready ()
{
if (xh.readystate==4)
{
if (xh.status==200)
{
odiv.innerhtml = "complete"
}
Else
{
odiv.innerhtml = "Sorry, load data failed." Reason: "+ xh.statustext
}
}
}//author:longbill www.longbill.cn

function Setcookie (name,value)
{
var cookiestr=name+ "=" +value+ ";";
var expires = "";
var cookieexp=60*60*1000;
var d = new Date ();
D.settime (D.gettime () + cookieexp);
expires = "expires=" + d.togmtstring () + ";";
Document.cookie = cookiestr+ expires;
}
function $ (a)
{
Return document.getElementById (a);
}
</script>

<body>
Ajax uses cookies to pass the value example:<br>
<form name=myform>
Name:<input id=name value= "variable name can even be Chinese" size=20><br>
Value:<input type=text size=20 id=val value= ><br>
<input onclick= "GetXML ()" type= "button" value= "Send out Data" >
<input onclick= "if (xh && xh.responsetext) {alert (xh.responsetext);}" type= "button" value= "Show return results" >< Br>
<div id=m bgcolor=blue> This display status </div>
<input Type=button onclick= "alert (document.cookie)" value= displays local cookie>
</form>

Server-side Code a.php
Copy Code code as follows:

?
Header ("content-type:text/html;charset=gb2312");
echo "Below is all the cookie variables and their value \ n" that you send out;
Print_r ($_cookie);
?>
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.