I am having trouble escaping the & in a JSON.stringfy() ajax call. I don't even know if I am stating the problem correctly here.
In my app I have linked json2.js from http://www.JSON.org/json2.js 2010-08-25(see attached file) to my app.
Then I have created an ajax call in the app to my server_side app in which I invoke the JSON.stringify( dataObj). My "dataObj" is an Associative Array. If one of the array element values contains the "&" the queryString variable is truncated. I was under the impression that JSON.stringify( ) would escape any control characters. but I am wrong.
What baffles me more is the fact I have even commented out the link to json2.js in my page header and the app does not crash when it get to the JSON.stringify( ) command.
I don't know if it is because I am using FireFox 3.6.13. I thought that I had read that they have now included JSON right in FireFox.
The question remains "How do I escape the & or any other control character, when it occurs in a value"? I would hope this would be part of the json process.
Here is the offending name/value pair:
"TITLE":"Earthw ork & Excavating Contracts Manager"
This is the string as transmitted
"TITLE":"Earthw ork
Here is my ajax code.
In my app I have linked json2.js from http://www.JSON.org/json2.js 2010-08-25(see attached file) to my app.
Then I have created an ajax call in the app to my server_side app in which I invoke the JSON.stringify( dataObj). My "dataObj" is an Associative Array. If one of the array element values contains the "&" the queryString variable is truncated. I was under the impression that JSON.stringify( ) would escape any control characters. but I am wrong.
What baffles me more is the fact I have even commented out the link to json2.js in my page header and the app does not crash when it get to the JSON.stringify( ) command.
Code:
<!-- <script type="text/javascript" language="JavaScript" src="json2.js"></script> -->
The question remains "How do I escape the & or any other control character, when it occurs in a value"? I would hope this would be part of the json process.
Here is the offending name/value pair:
"TITLE":"Earthw ork & Excavating Contracts Manager"
This is the string as transmitted
"TITLE":"Earthw ork
Here is my ajax code.
Code:
//a public array used to store the ajax call response
var aCallBack = new Array();
function myAjax (callApp, dataObj, getOrSave) {
this.makeCall = function (callApp, dataObj, getOrSave) {
var queryString = "&appData="+JSON.stringify(dataObj);
//create ajax object
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
//assemble data
var url = callApp+'?timeStamp=' + new Date().getTime();
//actually make call and get response in chkStateChange function
request.open("POST", url, true);
request.onreadystatechange = function chkStateChange(){
if (request.readyState == 4) {
if (request.status == 200) {
processResponse(request.responseText);
}
}
};
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send( queryString );
};
var processResponse = function (response) {
aCallBack = response.split(';');
if ( aCallBack[0] == "An error occured") {
eAlert = aCallBack[0]+"\n";
for (var i = 1; i < aCallBack.length; i++ )
{
eAlert += aCallBack[i]+"\n";
}
alert( eAlert );
}else if (aCallBack[0] == "Record Saved") {
alert("temp message Record Saved. /n look in _Navigation.js");
}else{
updatePage();
}
};
}
//ajax object created
var ajax = new myAjax();
Comment