i am trying to build this function data get an JavaScript object and process that some time it's need a call back function so i am trying pass that function inside the object but when i call this it's says function dosen't exist and yet still this function exist and when i pass this as parameter it's working this only happen when i get callback function name from JavaScript Object
function Ajax(data){
//ajax stuff
var filtered_respons = response_from_ajax;
//tryed this all not working
data['callback'](filtered_response,'loading');
var fun_name = data['callback'];
fun_name(filtered_response,'loading');//
}
and from html page i am calling this function as this
let data = {
url:'./assets/php/category.php',
type:'POST',
callback :'toprender',
data:{all_cats:1},
element_name:'cat'
}
Ajax(data);
And when i try this way it's working
AjaxCaller(data,topfunction,true);
This is the whole function
AjaxCaller(data,callback,need){
var loader= "";
if(typeof data['loader'] !== "undefined"){
loader = $("#"+data['loader']);
}else{
loader = $("#loading");
}
loader.show();
if(typeof data['url'] !== "undefined" && typeof data['type'] !== "undefined"){
var $self = this;
$.ajax({
url:data['url'],
type:data['type'],
data:data['data'],
success:function(response){
loader.hide();
var response = JSON.parse(response);
var filtered_response = $self.ajaxError(response);
if(need==true){
callback(filtered_response,data['element_name']);
}
}
});
}else{
console.log('Please Re Check The Objects');
}
}
You're trying to use a string as a function. You can't do that.
You probably just want to change callback :'toprender'
to callback :toprender
:
let data = {
url:'./assets/php/category.php',
type:'POST',
callback :toprender,
data:{all_cats:1},
element_name:'cat'
}
Ajax(data);
That sets callback
to the fucntion, rather than a string.
Live Example:
function toprender() { console.log("toprender called"); } function Ajax(data) { data.callback(); } let data = { url:'./assets/php/category.php', type:'POST', callback :toprender, data:{all_cats:1}, element_name:'cat' } Ajax(data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.