Skip to content Skip to sidebar Skip to footer

How To Make Ajax Call With Array And String Parameters

I want to make ajax call to my server with array value and sting parameters. This is my function I am using in my page. var globalArray = []; function submit() { var string_My

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"