阿贾克斯.done()不一起工作对我来说 [英] Ajax .done() don't work together for me

查看:127
本文介绍了阿贾克斯.done()不一起工作对我来说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常奇怪的情况。我有一个AJAX功能,发送表单数据的PHP codeigniter控制器,在JSON响应,它必须处理的响应。第一部分工作,但后来的一部分,这是一个.done()函数,不工作,不管我怎么努力。 这里是我的脚本:

I have a very strange situation. I have an AJAX function which sends form data to a php codeigniter controller, on json response, it has to deal with the response. first part is working, but later part, which is a .done() function, doesn't work, no matter what I try. here is my script:

var validator = $('#register-company-form').validate({
    rules: {
        title: {
            required: true,
            valueNotEquals: 0
        },
        /* rules here */
    },
    highlight: function (element) {
        $(element).closest('.form-field').addClass('error-field');
    },
    unhighlight: function (element){
        $(element).closest('.form-field').removeClass('error-field');
    },
    errorPlacement: function(error, element) {},
    submitHandler: function(form) {
        var formData = new FormData($(form)[0]);
        $.ajax({
            type: $(form).attr('method'),
            url: $(form).attr('action'),
            data: formData,
            dataType: 'json',
            cache: false,
            contentType: false,
            processData: false
        })
        .done(function (response) {
            $(".form-field").removeClass("error-field");
            $(".item-exists").hide();

            if(response.Response == 401) {
                 $("#company_email").closest('.form-field').addClass('error-field');
                 $("#company_email").closest(".form-field").find(".item-exists").show();
            } else if(response.Response == 402) {
                $("#personal_email").closest('.form-field').addClass('error-field');
                $("#personal_email").closest(".form-field").find(".item-exists").show();
            } else if(response.Response == 403) {
                $("#user_name").closest('.form-field').addClass('error-field');
                $("#user_name").closest(".form-field").find(".item-exists").show();
            } else if(response.Response == 200){
                /* load my view */
            }
        });
        return false;
    }
});

我的以下JSON响应的PHP脚本返回:

My PHP script returns following JSON response:

{响应:200,数据:空,消息:空}

{"Response":200,"Data":null,"Message":null}

得到这个答复后,我.done()函数应该根据其采取行动,并加载一个网页,它不是。我试图把执行console.log()和警报()进去,但现在它明确表示不响应。有没有做到这一点任何其他方式或code的任何修正? 请注意,在同一code真的另一台服务器上运行良好。这种迁移后发生。

After getting this response, my .done() function is supposed to act according to it and load a page, which it is not. I have tried putting console.log() and alert() into it, but now its clear its not responding. Is there any other way to do this or any correction in code? Please note that the same code really worked fine on another server. This has happened after migration.

感谢你这么多的帮助!

推荐答案

感谢你这么多您的实物资料 @DFreind @JonathanLonowski ,用你的提示,我终于想通了这个问题,这花了我将近3天。 其实,当我仔细研究了由PHP生成的HTML,它说:     1 {响应:200,数据:空,消息:空}

Thank you so much for your Kind information @DFreind and @JonathanLonowski, with your hints, I finally figured out this problem which took me almost 3 days. Actually when I looked closely at the html produced by PHP, it said: 1 {"Response":200,"Data":null,"Message":null}

这个'1'之前的JSON字符串中产生无法修改标题错误! 经过大量的努力,我刚才看到一个简单的该死的'1'刚刚开放&LT之前,在第一线在我的控制器的PHP 标签?删除此'1'的工作就像一个魅力,所有的错误走了,生命得救了,加药在天上现在: - )

This '1' before the JSON string was generating cannot modify headers error! After lots of efforts, I just saw a plain goddamn '1' just before opening <?php tag on first line in my controller. Removing this '1' worked like a charm, all errors gone, life saved, dosing in heaven now :-)

适应症的研究人员:请安装Firebug的,如果你正面临着​​类似的错误,总是考虑响应头,尝试尝试。大部分的时间PHP错误惹你的输出。就我而言,头已经发出生成的,因为PHP开始了它的输出之前的错误,HTML进来了。留意任何回音()或set_cookie()函数的!

Indication for researchers: Please install Firebug if you are facing similar errors and always look into response headers, try experimenting. Most of the time PHP errors mess with your output. In my case, 'headers already sent' error generated because before php started its output, html came in. Watch out for any echo() or set_cookie() functions as well!

感谢大家StackOverflowish爱好者:)

Thanks all of you StackOverflowish geeks :)

这篇关于阿贾克斯.done()不一起工作对我来说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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