AJAX请求jQuery无法正常工作

KAP*_*ILP 0 javascript jquery

function showDialog(link) {
    $('#dialog .newsletter-join').click(function() {
        email = $.trim($(this).parent().find('.email').val());
        if (email != "Email Address" && email.length > 5) {
            $.ajax({
                url: '/newsletter/join',
                data: {
                    'email': email,
                    'pid': $('#dialog .pid').html().trim()
                }
            });

            return true;
        }
        return false;
    });
}
Run Code Online (Sandbox Code Playgroud)

这段代码在FF和chrome中正确执行,在调试模式下执行时运行完美,否则它不会返回任何内容.

执行时,它显示警报框,其中没有任何内容.我在控制台中的XMLHttp响应中也没有看到任何内容.我尝试了一些其他的改变:

cache:false // It just exeutes for the first time 
datatype:html 
Run Code Online (Sandbox Code Playgroud)

你能告诉我如何追踪这个问题吗?

更新的代码:

我使用以下脚本调用代码

$(document).ready(function() {
    // $(window).load(function () {
    showDialog();
    return true;
    //});
});

function showDialog(link) {
    $('#dialog .newsletter-join').click(function() {
        email = $.trim($(this).parent().find('.email').val());
        if (email != "Email Address" && email.length > 5) {
            $.ajax({
                url: '/newsletter/join',
                data: {
                    'email': email,
                    'pid': $('#dialog .pid').html().trim()
                },
                success: function(data) {
                    alert("Suvvess" + data);
                },
                error: function(xmlHttpRequest, textStatus, errorThrown) {
                    alert("error" + errorThrown);
                }
            });

            return true;
        }
        return false;
    });
}
Run Code Online (Sandbox Code Playgroud)

最新代码更改:我添加了

  async: false
Run Code Online (Sandbox Code Playgroud)

ajax调用中的这种更改是给出了预期的结果,但这会阻止所有脚本执行,所以仍觉得这不能成为永久的解决方案

Sua*_*Nti 6

你应该在出错后关闭ajax函数

$.ajax({
                url:'/newsletter/join',
                data:{ 'email':email, 'pid':$('#dialog .pid').html().trim() }

  >>>HERE REMOVE IT        });
Run Code Online (Sandbox Code Playgroud)