1

I'm trying to do something but with no success. 1. I want to do an ajax call by jquery and to pass an array (i assumed it should be on the data property). 2. Then, i want to retrieve this array on the server side but im pretty lost how.

Can anyone please explain how can this be easily done? I thought about using context.Request.Params but im guessing this is not the right way..

var array = [];
...

 $.ajax({
      cache: false,
      method: 'json',
      url: 'handler/myhandler.ashx',
      data: array
2
  • Your endpoint is an asp.net generic handler? (guess since you wrote "handler" as url). Commented Dec 8, 2012 at 10:18
  • Yes, is that okay? i just updated my question writing the file extension ashx Commented Dec 8, 2012 at 10:20

2 Answers 2

2

EDIT: I just realized you need to "post" your data. You should add "type: 'POST'" to your ajax call :)

 $.ajax({
      type: 'POST',
      cache: false,
      method: 'json',
      url: 'handler/myhandler.ashx',
      data: array

You are correct, your array should be inserted into the "data" property of the ajax call.

You can access the data on the server side through the HttpContext in your handler. There is an attribute called request.

For converting the object into an object, here's a deserializsation example. Where it is deserialized into a dictionary.

public void ProcessRequest(HttpContext context)
{
    var data = context.Request;
    var sr = new StreamReader(data.InputStream);
    var stream = sr.ReadToEnd();

    var javaScriptSerializer = new JavaScriptSerializer();

    var arrayOfStrings = javaScriptSerializer.Deserialize<string[]>(stream);
Sign up to request clarification or add additional context in comments.

11 Comments

I tried the context.Request.Form["json"] way, still having problems on the server side.. and in general, how can this data object can be converted to an array on that side? after all i don't wanna keep it as some json object
First thank you for ur fast reply. I just tried what u wrote, the only problem is im getting an exception in the last line: '...is not supported for deserialization of an array.'
The first step would be to find the string you are looking for in the Request (I usually use debugging for this). As it will be send as a string to the server. Then the server has to use deserialisation to turn it into an object (like an array of strings). Can you post the string you are trying to convert to an array?
Sure, for example, my stream (after the readToEnd() method) has this: [["1","2","3"],["4","5","6"],["7","8","9"],[]]
I just tried. Works great!! What a great help thank you very much. If i'll have more problems i promise to annoy you again :)
|
0

using JSON.stringify() like this:

 $.ajax({
      cache: false,
      method: 'json',
      url: 'handler/myhandler.ashx',
      data: JSON.stringify(array),
      type: "POST"

After data gets send to the server side, all you need to do now is to deserialize the object, this is how you do it

//might be something else other than Forms["json"], use debug mode to figure out the param
    string json = HttpContext.Current.Request.Forms["json"];
    JavaScriptSerializer  serializer= new JavaScriptSerializer();
    var urObj = serializer.Deserialize<Type>(json);

1 Comment

How can i retrieve this data?

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.