javascript - 使用 $.post 并将返回函数作为带有附加参数的变量传递

标签 javascript jquery

我有一个主要的 post 函数,用于所有 post 调用:

function post_json(post_string, response_handler) {
    //
    // do logging and other things with post string
    //
    $.post(post_string,{},response_handler,'json');
}

然后我将在各个页面中使用此调用:

post_json(post_url, handler_generic); 

handler_generic函数是这样的:

function handler_generic(json) {       
    var success = json.success;
    var success_verbiage = json.success_verbiage;
    // do lots of stuff here   
}

这非常有效。

我想做的是有一个这样的功能:

function handler_generic_with_extra(json, unique_id) {       
    var success = json.success;
    var success_verbiage = json.success_verbiage;
    // do lots of stuff here and now do it with the 
    // unique id  
}

我认为这些行不通,而且它们也行不通:

 post_json(appended_post_string, handler_generic_with_extra( the_unique_id));

 post_json(appended_post_string, handler_generic_with_extra( json, the_unique_id));  

如果不创建新的 post_json 函数来处理这些情况,有什么办法可以实现这一点吗?

最佳答案

使用闭包:

post_json(appended_post_string,function(json){return handler_generic_with_extra( json, the_unique_id); }); 

关于javascript - 使用 $.post 并将返回函数作为带有附加参数的变量传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40898741/

相关文章:

javascript - 毫秒到各种 GMT 时区转换

javascript - 无法将 html5 Canvas 签名板提交到数据库

javascript - npm 运行构建 uglifyjs : SyntaxError: Unexpected token: operator (>)

php - 使用 jQuery 和 CodeIgniter PHP MVC 自动完成

javascript - 如何让按钮漂浮在剑道小部件上

javascript - 将外部 jQuery 文件包含在 $(document).ready() 中

javascript - jQuery 响应式图像映射 : Uncaught TypeError

javascript - 如果数组位于使用 map 方法的对象中,如何从数组中获取数据?

jquery - 单击图像时响应式菜单切换

javascript - Jquery动态div多个框并放入数据库