jquery - 如何处理jquery ajax注释和验证码

标签 jquery ajax captcha

我正在社交网络上开发评论系统,我正在使用jquery,我可以毫无问题地使用ajax发布评论,但有时我需要用户提交验证码表单,如果他们发布太多评论或为了其他原因。

我认为最好的方法是将其添加到当前评论发布部分,如果php脚本返回响应,说明我们需要做一个验证码表单,那么我想自动打开一个对话框屏幕上的窗口,让用户填写验证码表单,然后继续并在那里发布评论。

这对我来说有点复杂,但我想我已经完成了大部分工作,也许你可以阅读我下面的评论并帮助我解决验证码部分,主要是关于我如何触发一个对话框打开,我如何通过通过验证码评论值/文本,成功后再次返回评论,如果用户获取验证码错误,则会重新加载验证码

$.ajax({
    type: "POST",
    url: "processing/ajax/commentprocess.php?user=",
    data: args,
    cache: false,
    success: function (resp) {
        if (resp == 'captcha') {
            //they are mass posting so we need to give them the captcha form
            // maybe we can open it in some kind of dialog like facebox
            // have to figure out how I can pass the comment and user data to the captcha script and then post it
        } else if (resp == 'error') {
            // there was some sort of error so we will just show an error message in a DIV
        } else {
            // success append the comment to the page
        };
    }
});

最佳答案

我想我会选择使用 modal dialog that comes with the jQuery UI library 。然后,我将 AJAX 调用包装在一个函数中,这样我就可以递归地调用它。我将创建一个 DIV (#captchaDialog) 来处理显示验证码图像和一个用于输入答案的输入 (#captchaInput)。当用户单击模式对话框上的“确定”按钮时,我将使用新的验证码响应修改原始参数并调用该函数。由于此解决方案只是修改原始参数并将其重新传递到相同的 URL,因此我相信此解决方案适合您。

一些示例代码,减去模态对话框的 div 和输入:

var postComment = function(args) {
    $.ajax({
        type: "POST",
        url: "processing/ajax/commentprocess.php?user=",
        data: args,
        cache: false,
        success: function (resp) {
            if (resp == 'captcha') {
                $("#captchaDialog").dialog({
                    bgiframe: true,
                    height: 140,
                    modal: true,
                    buttons: {
                     ok: function() {
                        args.captchaResponse = $(this).find("#captchaInput").val();
                        postComment(args);
                     }
                    }
                });
            } else if (resp == 'error') {
                // there was some sort of error so we will just show an error message in a DIV
            } else {
                // success append the comment to the page
            };
        }
    });
};

希望这有帮助!

关于jquery - 如何处理jquery ajax注释和验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1257991/

相关文章:

javascript - SetInterval中的长AJAX请求如果长于间隔是否会终止

authentication - 如何使用 GMail 作为免费的 SMTP 服务器并克服验证码

php - 验证码非 PHP

javascript - 由于验证码需要按提交按钮两次

javascript - 雨量雷达在谷歌地图中的位置

android - 复选框 onchange 事件 android cordova visual studio 不工作

jquery - 通过 jquery-tmpl 中的函数调用附加动态 HTML 元素

jquery - Googlebot 看不到 jquery 生成的内容

javascript - PHP,AJAX联系表上的Captcha系统问题

ajax - Chrome 在重定向时取消 CORS XHR