繁体   English   中英

如何使用prototypejs ajax onSuccess事件调用现有的已定义JavaScript函数?

[英]How to call an existing defined javascript function with an prototypejs ajax onSuccess event?

我能找到的所有文档都让您定义了像这样的ajax构造函数中要调用的实际函数

new Ajax.Request('/your/url', {
   onSuccess: function(response) {
   // Handle the response content...
}
});

但是我的代码抛出一个错误,提示“未捕获的typeError:未定义不是函数”这是我的代码试图调用现有函数pullComments()。

    function addComment(){
    //  Stringify json 
    var commentStr = $('#commentBox').value;
    var JSONObj = { "dealID" : dealID,
                    "username" : "default",
                    "comment" : commentStr };
    JSONObj = JSON.stringify(JSONObj);
    var JSONComment = encodeURIComponent(JSONObj);
    new Ajax.request("php/savecomment", 
    {
        method: 'POST',
        parameters: "comment=" + JSONComment,
        onSuccess: pullComments()
    });
}

function pullComments(){
    //  Grab the comments for the deal
    var xmlhttp
            ****

改成:

onSuccess: pullComments

您想将函数的实现传递给“ onSuccess”,而不是函数的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM