How To Make Ajax Call With Array And String Parameters
Solution 1:
Javascript offers methods for serialization:
toJSON()
This function is used to define what should be part of the serialization. Basically you can create a clone of the object you want to serialize excluding cyclic dependencies or data that should not be send to the server.
More information on this behaviour can be found here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior
JSON.stringify()
This function calls toJSON() and serializes the returned object.
Example:
var objectToSend = {};
objectToSend.varX = 5;
objectToSend.varY = 10;
objectToSend.toJSON = function() { return {varX: this.varX}; }
var jsonString = JSON.stringify(objectToSend);
The result will be:
"{"varX":5}"
Solution 2:
If you are writing both the client and server implementations then I would advise creating your data as a JSON object then using base64 encoding to convert the JSON object to a string that you can send as one encoded parameter:
encodededjson=(BASE 64 encoded JSON here)
Then on the server you just use the equivalent base 64 decode routine to translate the encoded data back to JSON. So you use:
var strJSON = JSON.stringify(globalSelection);
And pass the result of this to the base64 encode routine, then use the result from that to build your Post:
var strJSON = JSON.stringify(globalSelection);
,strEncoded = Base64.encodeBase64(newString(strJSON.getBytes()))
,strPost = "datatopost=" + strEncoded;
Post a Comment for "How To Make Ajax Call With Array And String Parameters"