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);
}
});
}
asked Feb 28, 2013 at 11:26
kamal
2371 gold badge6 silver badges19 bronze badges
1 Answer 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);
}
});
}
answered Feb 28, 2013 at 11:40
MathKimRobin
1,6005 gold badges27 silver badges60 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
kamal
it's actually returning data as json and works fine. but is it really okey to use it like this?
MathKimRobin
no, prefer ask for datatype:'json' in your request and give callbacks (success and fail functions) to your c_loadAjax as parameters
lang-js
.done