-1

Continuing from this post of mine passing id to ajax call

There is still something I would like to understand more.

My problem is like this

I have 2 pages (default.cshtml+click.cshtml) In default.cshtml, I have a js code to initialize a div region with its id some image. When I click the link as mentioned in my previous post I need to change the image in this region because the new image is loaded from the database. Each image is associated with an assigned id; now that I don't know how or where I should put this initialize script because it is used by both default and click pages

function init(id)
{
   //...initialize options
   var id=document.GetElementByID("displayimg");
   //...display image
}

How can I implement the success handler in java script that has 2 or more parameters ?

$(document).ready(function ()
{
    $('.viewp').click(function ()
    {
        var responseUrl="~/click?id="+id;                    
        $.ajax(
        {
            type: "GET",
            data:id,
            url:responseUrl,
            success:function(data1, data2)
            {

            }
        });
    });
});

because the click.cshtml will query the database table for 2 more data values to redisplay the image, it is its new width and height :-D

Community
  • 1
  • 1
  • 1
    Just create a DTO or return an anonymous type in JSON format wrapping all the properties you want to return to the AJAX call – Jupaol Sep 22 '12 at 19:17
  • Have a look at the docs: http://api.jquery.com/jQuery.ajax/ The section on `success` explains the expected function parameters. The `data` parameter will contain all the data. If you have more than 1 value then wrap them into a single object to be returned. No matter how much data you return, it only can come back as a single object through the `data` parameter. – Nope Sep 22 '12 at 19:32

3 Answers3

3

The success function must be in the form: function (data, textStatus, jqXHR). Which means, whatever the server outputs is in data. You will have to parse data.responseText to get your data1 and data2.

Lian
  • 2,197
  • 2
  • 15
  • 16
3

I'd respond from your server with JSON, so something like:

{
    height: 20,
    width: 20
}

Then in your ajax call, you can just do

$.ajax({
    url: url,
    dataType: "json",
    success: function( dimensions ) {
        // Do something with dimensions.width and dimensions.height
    }
});
dherman
  • 2,832
  • 20
  • 23
1

put height and width into an array and send it to ajax.

in c# code

int[] val = { 10, 20 };

and in js ajax success function

success: function(value) { 
//  process value[0] which is height or width.... value[1] etc.
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221