1

I am trying to return a json object from a function and use it as the below code, but it's not working. what wrong with it?

var x = [ "EditFileName" , "dosometing" ];
c_loadAjax.apply(this,x).done(function(json){
    alert(json.error);
});
function c_loadAjax( post , option ){
    $.ajax({
        type:"POST",
        url:"/includes/Ajax.php",
        data:{post:post,option:option},
        error:function(result){
            return '{"error":"Error"}';
        },
        success:function(result){
            return jQuery.parseJSON(result);
        }
    });
}
1
  • 1
    I think you have to return the deferred object itself to use it with .done Commented Feb 28, 2013 at 11:29

1 Answer 1

1

Try with the return keyword

var x = [ "EditFileName" , "dosometing" ];
c_loadAjax.apply(this,x).done(function(json){
    alert(json.error);
});
function c_loadAjax( post , option ){
    return $.ajax({
        type:"POST",
        url:"/includes/Ajax.php",
        data:{post:post,option:option},
        error:function(result){
            return '{"error":"Error"}';
        },
        success:function(result){
            return jQuery.parseJSON(result);
        }
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

it's actually returning data as json and works fine. but is it really okey to use it like this?
no, prefer ask for datatype:'json' in your request and give callbacks (success and fail functions) to your c_loadAjax as parameters

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.