0

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.

7
  • What's the value of "argumentValues" before it gets sent to the service? Commented Sep 21, 2012 at 8:21
  • The value of argumentValues is:rgumentValues: Array[3] 0: "1111aa" 1: "BE" 2: "24041" length: 3 Commented Sep 21, 2012 at 8:23
  • is the jsonCallbackID sending through all fine? The serialization of the jsonCallbackvalues should be all fine from what I can see. Commented Sep 21, 2012 at 8:29
  • 1
    By using getJSON you are basically executing an ajax call with content type of json. That will trigger the model-binder on the server side to assume all the values you are sending up are in JSON serializable format. If they are not than the JSON model-binder on the server-side is unable to populate your parameters correctly, hence null. If you are telling it you are passing JSON than you need to pass JSON serializable parameters. Commented Sep 21, 2012 at 8:37
  • 1
    @Arthur: getJSON is the same as $.ajax({ url: url, dataType: 'json', data: data, success: callback }); If you want more control you can use .ajax() directly. api.jquery.com/jQuery.ajax There you can specify the exact content type and so on. In your scenario though if getJSON and $.toJSON works, then happy days :) Commented Sep 21, 2012 at 8:57

3 Answers 3

3

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) {});
Sign up to request clarification or add additional context in comments.

1 Comment

I did this indeed, the result is a comma seperated list in the jsonCallbackValues argument, guessing from Spekdrum's answer below, this is the way to go. Thanks!
2

JSON is a serialized language, so you can't put objects inside.

You should build your array in JSON format:

jsonCallbackValues : ["value1", "value2"...]

1 Comment

Thanks! I did try something similar as a workaround, but it seems from your question that this is the only option I have.
2

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 []

1 Comment

Yeah, this is similar to the answer above, I'm serializing / de-serializing in a similar fashion now. But thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.