I'm currently writing a query that needs to send a string array to a webresource through json. This is the webresource i need to call:

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

Result API.ImportByNumbers(string[] _Numbers) {}

Now i dont know how to form my json data. I've tried sending a normal javascript string array with all the numbers in it, but this gives a "Could not process child error".

When i try an array of objects

Data = '{"ID":"1", "ID":"2"}' 

the value inside webresource is always null...

Can anybody help?

share|improve this question
Data = '{"ID"="1", "ID"="2"}' isn't valid JSON. – Waleed Khan Sep 3 '12 at 14:16
try with '{"ID":"1","ID":"2"}' – Onheiron Sep 3 '12 at 14:17
sorry my bad, i was allready using ':' instead of '='. – user1643956 Sep 3 '12 at 14:44
feedback

3 Answers

up vote 1 down vote accepted

Ok. I found the solution:

var idArray = ...        array of strings    
var Data = {"_Numbers": idArray }

then in in the query:

Data = JSON.stringify()
share|improve this answer
feedback

Since your webmethod needs string[] _Numbers SO you have to pass such a json data which will send string of number .. Something like this.

var string={"1","2","3"};

var jsonData="{"+"_Numbers:"+"'"+string+"'"+"}"
share|improve this answer
I have also tried that... And the value for _Numbers is always null then. – user1643956 Sep 3 '12 at 14:41
can you post your ajax code? – Ashirvad Sep 3 '12 at 14:42
$.ajax({ type: Type, url: Url, data: Data, contentType: ContentType, dataType: DataType, processdata: ProcessData, success: function (msg) { debugger; alert("import successfull") }, error: ServiceFailed }); – user1643956 Sep 3 '12 at 14:47
All the values should be ok cause im alrdy using them succesfully for a other method. – user1643956 Sep 3 '12 at 14:48
Type = "POST"; Url = "http://..."; ContentType = "application/json"; DataType = "json"; Data = '{"_Numbers" : "1", "_Numbers" : "2"}' varProcessData = true; – user1643956 Sep 3 '12 at 14:54
show 1 more comment
feedback

Looks like the service is expecting to receive an array, not an object, so you should try not to complicate things and send something like this:

var json_str = "[1,2,3]"

But if you really need an JSON object, then you should try this

var json_str='{"_Numbers":[1,2,3]}'
share|improve this answer
1  
i have tried both your answers and they don't work: the first one gives an error back: expected value type and object so i guess it needs a json object the 2nd gives an error: expecting '/' found '_', could this be due to the name _Numbers beginning with _? – user1643956 Sep 4 '12 at 7:01
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.