Ajax 成功事件不起作用 [英] Ajax success event not working

查看:30
本文介绍了Ajax 成功事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册表单,正在使用 $.ajax 提交.

I have a registration form and am using $.ajax to submit it.

这是我的 AJAX 请求:

$(document).ready(function() {
    $("form#regist").submit(function() {
        var str = $("#regist").serialize();
        $.ajax({
            type: 'POST',
            url: 'submit1.php',
            data: $("#regist").serialize(),
            dataType: 'json',
            success: function() {
                $("#loading").append("<h2>you are here</h2>");
            }        
        });
        return false;        
    });
});

在我的 submit1.php 文件中,我检查数据库中是否存在 电子邮件地址用户名 字段.如果这些值存在页面刷新,我希望显示错误消息.

In my submit1.php file I check for the existence of fields email address and username in the database. I wish to display an error message if those value exist without a page refresh.

如何将其添加到我的 AJAX 请求的 success 回调中?

How can I add this to the success callback of my AJAX request?

推荐答案

结果可能不是 JSON 格式,因此当 jQuery 尝试解析它时,它失败了.您可以使用 error: 回调函数捕获错误.

The result is probably not in JSON format, so when jQuery tries to parse it as such, it fails. You can catch the error with error: callback function.

无论如何,您似乎不需要在该函数中使用 JSON,因此您也可以取出 dataType: 'json' 行.

You don't seem to need JSON in that function anyways, so you can also take out the dataType: 'json' row.

这篇关于Ajax 成功事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆