Return two or more values ​​from a function

I need to return multiple values ​​from a ColdFusion function to an ajax callback function. Here is what I have:

$('input[name="StateName"]').live('change', function() {
    var StateID = $(this).parents('tr').attr('id');
    var StateName = $(this).val();
    $.ajax({
        url: '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 catch a database error, then the success callback is executed and the result is not a number (actually this is the text of the error message). I need a function to pass other values. One of them may be the primary key of the line that caused the error. The old StateName may be another, so I can update the old value on the screen so that the client knows exactly that their changes have not taken effect.

, , , , . , .

+5
3

** JSON string **, , .

"".

JSON Javascript Object Notation, Javascript - JSON .


, JSON:

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

3 :

  • status
  • message
  • data; , .

AJax - .

+6

ajax . : Success, Data Errors.

jsonify -, , , ( ), , . , .

, Errors -. jQuery. , , "success" :

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

You can return the xml string and then access it in the 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;
            }

}
+1
source

All Articles