简体   繁体   English

Ajax .done()对我不起作用

[英]Ajax .done() don't work together for me

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. 我有一个AJAX函数,它将JSON响应上的表单数据发送到php codeigniter控制器,它必须处理响应。 first part is working, but later part, which is a .done() function, doesn't work, no matter what I try. 第一部分工作正常,但是后面的部分是.done()函数,无论我尝试什么,都无法工作。 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;
    }
});

My PHP script returns following JSON response: 我的PHP脚本返回以下JSON响应:

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

After getting this response, my .done() function is supposed to act according to it and load a page, which it is not. 收到此响应后,我的.done()函数应该按照它的作用并加载一个页面,而实际上不是。 I have tried putting console.log() and alert() into it, but now its clear its not responding. 我尝试将console.log()和alert()放入其中,但是现在它显然没有响应。 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. 迁移后发生了这种情况。

Thank you so much for the help! 非常感谢你的帮助!

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. 非常感谢您提供的@DFreind@JonathanLonowski信息,在您的提示下,我终于弄清楚了这个问题,这使我花了将近3天的时间。 Actually when I looked closely at the html produced by PHP, it said: 1 {"Response":200,"Data":null,"Message":null} 实际上,当我仔细查看PHP产生的html时,它说:1 {“ Response”:200,“ Data”:null,“ Message”:null}

This '1' before the JSON string was generating cannot modify headers error! JSON字符串生成之前的此“ 1”无法修改标头错误! After lots of efforts, I just saw a plain goddamn '1' just before opening <?php tag on first line in my controller. 经过大量的努力,在控制器的第一行打开<?php标签之前,我只是看到一个普通的该死的'1'。 Removing this '1' worked like a charm, all errors gone, life saved, dosing in heaven now :-) 删除这个“ 1”就像一个魅力,所有错误消失了,挽救了生命,现在在天上服用:-)

Indication for researchers: Please install Firebug if you are facing similar errors and always look into response headers, try experimenting. 研究人员的建议:如果您遇到类似的错误,请安装Firebug,并始终查看响应标头,然后尝试进行实验。 Most of the time PHP errors mess with your output. 大多数时候,PHP错误会干扰您的输出。 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! 在我的情况下,由于在php开始输出之前,html产生了“已发送标头”错误。请注意所有echo()或set_cookie()函数!

Thanks all of you StackOverflowish geeks :) 谢谢大家StackOverflowish怪胎:)

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

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