开发者

Returning two or more values from a function

I need to return multiple values from a ColdFusion function in an ajax callback function. Here's what I've got:

$('input[name="StateName"]').live('change', function() {
    var StateID = $(this).parents('tr').attr('id');
    var StateName = $(this).val();
    $.ajax({
        ur开发者_开发百科l: 'Remote/State.cfc'
        ,type: "POST"
        ,data: {
            'method': 'UpdateStateName'
            ,'StateID': StateID
            ,'StateName': StateName
        }
        ,success: function(result){
            if (isNaN(result)) {
                $('#msg').text(result).addClass('err');
            } else {
                $('#' + result + ' input[name="StateName"]').addClass('changed');
            };
        }
        ,error: function(msg){
            $('#msg').text('Connection error').addClass('err');
        }
    });
});

If I trap a database error, then the success callback is fired, and the result is Not a Number (It is in fact, the text of the error message). I need the function to also pass back other values. One might be the primary key of the row that caused the error. Another might be the old StateName, so that I can refresh the old value on the screen so that the client will know absolutely for sure that their change did not take effect.

I guess I'm breaking the rule of atomicity here and need to fix that, because I'm using result as both the primary key of the row that was updated, or it's the error message if the update fails. I need to return both the primary key and the error message.


Your remote function could return a **JSON string**, containing an object, I suppose.

It would allow you to have several values inside a single "value".

And JSON being Javascript Object Notation, it's quite easy to read it in Javascript -- and there are libraries to serialize data to JSON in lots of languages.


For example, here's a JSON string :

{"status":1,"message":"this is a message","data":{"test":"plop","hello":"world"}}

This corresponds to an object with 3 properties :

  • status
  • message
  • and data ; which is itself another object, that contains two properties.

Returning this kind of string from your AJax call should not be too hard -- and that's a pretty flexible, and really prowerful, system.


I make all my ajax requests return the same object type. The pattern I use is a pretty common one - my response object always consists of a Success flag, a Data property, and an Errors collection.

If you jsonify an object like that and return it for all your ajax requests, you can always determine if the request was successful, what the errors were (if any), and if it was successful, you'll have the resulting data. In this way, you'll always be able to handle your responses in a consistent manner.

Note that the Errors collection would be business logic errors - actual server errors would still trigger the jQuery failure handler. But using the above object, your "success" function looks more like:

 if (!result.Success) {
    $('#msg').text(result.Errors[0]).addClass('err');
 } else {
    $('#' + result.Data + ' input[name="StateName"]').addClass('changed');
 };


You can return xml string and then access it in success function:

success:function(data)
{
  var err_code,err_msg;
  //read err_code and err_msg:
  // we need 2 different handlers for IE and all other browsers.
            if (! $.browser.msie) 
            {
                // this code surprisingly doesn't work with IE.
                err_code = $('error_code:first',data).text();
                err_msg = $('error_message:first',data).text();
            }
            else
            {
                var xml = new ActiveXObject("Microsoft.XMLDOM");
                xml.async = false;
                xml.loadXML(data);
                err_code = xml.documentElement.getElementsByTagName('error_code')[0].text;                    
                err_msg = xml.documentElement.getElementsByTagName('error_message')[0].text;
            }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜