Reputation: 134
I'm trying to pass a couple of arguments to my JSON callback, however the string[] argument remains null. How do I need to pass the string array from javascript to get this working?
Javscript function:
function jsonCallback(jsonCallbackID, argumentsArray) {
var argumentValues = [];
for (var i = 0; i < argumentsArray.length; i++) {
argumentValues.push('' + $("#" + argumentsArray[i]).val());
}
// build request
var url = encodeURI("/DataEntry/JsonCallback/");
var data = ({ jsonCallbackID: jsonCallbackID, jsonCallbackValues: argumentValues, rndvalue: Math.floor(Math.random() * 10000000001) });
// fire request
$.getJSON(url, data, function (data) {});
The actual callback C# function:
public JsonResult JsonCallback(int jsonCallbackID, string[] jsonCallbackValues)
{ }
This function does get called however, argument 'jsonCallbackValues' is null.
EDIT
To get this working I did the following:
var data = ({ jsonCallbackID: jsonCallbackID, jsonCallbackValues: argumentValues.toString(), rndvalue: Math.floor(Math.random() * 10000000001) });
And split the jsonCallbackValues string at "," to get the resulting array.
Upvotes: 0
Views: 7986
Reputation: 22619
Try like this
var data = ({ jsonCallbackID: jsonCallbackID, jsonCallbackValues: $.toJSON(argumentValues), rndvalue: Math.floor(Math.random() * 10000000001) });
I have used a JSON jQuery plugin from http://code.google.com/p/jquery-json/
In the controller method change it to string jsonCallbackValuesArray
Then use JavaScriptSerializer orJSON.Net to convert the JSON string into String []
Upvotes: 2
Reputation: 3456
You can give a try to JSON API ...
var data = {
jsonCallbackID:jsonCallbackID,
jsonCallbackValues: JSON.stringify(argumentValues),
rndvalue: Math.floor(Math.random() * 10000000001)
};
// your
$.getJSON(url, data, function (data) {});
Upvotes: 3
Reputation: 1767
JSON is a serialized language, so you can't put objects inside.
You should build your array in JSON format:
jsonCallbackValues : ["value1", "value2"...]
Upvotes: 2