1

I'm trying to query a database that contains information about a "ticket" using jQuery's .ajax() method...

$.ajax({
    type: 'GET',
    url: 'Preview.ashx',
    data: 'ticketID=' + ticketID,
    success: function (data) {

        // 'data' should be a row from the database, so it should be like an
        // array that contains each column of the row
        // do stuff with this data
    }
});

... so that all works fine. I'm having trouble with the data variable. On the server side, I do...

// get the ticket ID from the POST parameter
int ticketID = context.Request["ticketID"] != null ? Convert.ToInt32(context.Request["ticketID"]) : -1;
if (ticketID >= 0) {

    // grab the data from the database, getInfo() will retrieve the row
    // in the DB that corresponds to the ticket ID given, returning an 
    // ArrayList with all of the information
    ArrayList theTicket = getInfo(context, ticketID);

    // now, I need to somehow return this information so that I could deal with it
    // in the 'success' callback function above
    return;
} else {

    // something went wrong with the 'newTicket' POST parameter
    context.Response.ContentType = "text/plain";
    context.Response.Write("Error with 'ticketID' POST parameter. \n");
    return;
}
return;

I've debugged this enough to be sure that the ArrayList contains the correct information. Now I just need to return it.

How would I do this? How would I return the data in the ArrayList? Is it possible to structure the response so that I could do data.ID, data.otherColumnName, etc... in the callback function to access the different fields?

Hristo
  • 45,559
  • 65
  • 163
  • 230
  • is it possible to use any java scrip lib? json would be a good keyword here. If not, convert the data into xml, send it as text and parse it on the client. – user492238 Apr 05 '11 at 14:16

2 Answers2

3

Yes it is possible. Take a look at JSON. You can use JavascriptSerializer or DataContractJsonSerializer classes to serialize objects in JSON format. Also take a look at this link

You could use ContentType application/json and body will be string that one of the above serializers would return. You could use same code that is in your else statement

archil
  • 39,013
  • 7
  • 65
  • 82
  • @archil... 1. `this link` doesn't work. 2. can you provide an example of building such a response? – Hristo Apr 05 '11 at 14:22
  • @StuperUser... I'm not quite worried about parsing on the client side yet. I'd like to figure out how to create the response first, then worry about parsing it. If I don't have to parse it, the better, but I want to get that up and running first. – Hristo Apr 05 '11 at 14:27
  • I edited my answer. See the second paragraph and link too. The case is same es your else statement, you need content type and response text. – archil Apr 05 '11 at 14:28
1

You are already doing the correct thing below, with Response.Write.

I would change GetInfo to return an Array of strings, and then use String.Join.

string[] theTicket = getInfo(context, ticketId);
Response.Write(String.Join(",", theTicket);

otherwise you could loop through your arraylist and build the comma separated list yourself. then you can parse the comma separated values in js.

The better option, would be to use a json library and serialize the ArrayList as json and use that on the client side.

NerdFury
  • 18,876
  • 5
  • 38
  • 41
  • Yeah... I don't want to do any parsing. There should be a way to just return it as an Object, like if I were to use JSON. However, I want to avoid JSON for now... is there a way to do it without outside libraries? – Hristo Apr 05 '11 at 14:21
  • @Hristo - No. You are going across application boundaries and so serialization is necessary. JSon and XML are your standard serialization options. What are you trying to do with the data when it gets to the client? Another option would be to render the html you want on the server and return that, then use jquery to place the result on the page where you want it. That would be the quickest and easiest solution. – NerdFury Apr 05 '11 at 14:34
  • @NerdFury... gotcha. You're totally right. I'm going with the JSON solution. I'm just trying to query a row from the database and use the fields of the row to display information. – Hristo Apr 05 '11 at 14:38