1

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?

3
  • Data = '{"ID"="1", "ID"="2"}' isn't valid JSON. Commented Sep 3, 2012 at 14:16
  • try with '{"ID":"1","ID":"2"}' Commented Sep 3, 2012 at 14:17
  • sorry my bad, i was allready using ':' instead of '='. Commented Sep 3, 2012 at 14:44

3 Answers 3

1

Ok. I found the solution:

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

then in in the query:

Data = JSON.stringify()
0

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+"'"+"}"
5
  • I have also tried that... And the value for _Numbers is always null then. Commented Sep 3, 2012 at 14:41
  • $.ajax({ type: Type, url: Url, data: Data, contentType: ContentType, dataType: DataType, processdata: ProcessData, success: function (msg) { debugger; alert("import successfull") }, error: ServiceFailed }); Commented Sep 3, 2012 at 14:47
  • All the values should be ok cause im alrdy using them succesfully for a other method. Commented Sep 3, 2012 at 14:48
  • Type = "POST"; Url = "http://..."; ContentType = "application/json"; DataType = "json"; Data = '{"_Numbers" : "1", "_Numbers" : "2"}' varProcessData = true; Commented Sep 3, 2012 at 14:54
  • pass the data like i gave you.. mark there is single inverted comma before my string array Commented Sep 3, 2012 at 14:56
0

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]}'
1
  • 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 _? Commented Sep 4, 2012 at 7:01

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.