Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中正确使用else_Javascript_Jquery_Ajax - Fatal编程技术网

如何在javascript中正确使用else

如何在javascript中正确使用else,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个使用jqueryajax提交表单的脚本,除了我只能得到两个响应之外,一切都很好,加载部分已经实现,但其余部分我只能得到else语句。否则,没有一个在工作 json语句工作得很好。数据被成功地传递到php,但响应不一致 (function($) { 'use strict'; const FormFunction = function(){ const checkSelectorExistence = function(sel

我有一个使用jqueryajax提交表单的脚本,除了我只能得到两个响应之外,一切都很好,加载部分已经实现,但其余部分我只能得到else语句。否则,没有一个在工作

json语句工作得很好。数据被成功地传递到php,但响应不一致

 (function($) {

        'use strict';

        const FormFunction = function(){

            const checkSelectorExistence = function(selectorName) {
                if(jQuery(selectorName).length > 0){return true;}else{return false;}
            };



            let  registerForm = function()  {
                if(!checkSelectorExistence('.registration-form')){return;}
                jQuery('.registration-form').on('submit', function( event ) {
                    event.preventDefault();
                    let response        = $('.loading').addClass('show').show();
                    jQuery(this).find(".message").addClass('active').show('slow');

                    const formData      =   new FormData(this);
                    const formAction        =   jQuery(this).attr('action');

                    jQuery.ajax({
                        type: 'POST',
                        url: formAction,
                        data: formData,
                        contentType: false,
                        cache: false,
                        processData:false,
                        dataType: 'json',

                        beforeSend : function(){
                            $('.info').addClass('show').show('slow');
                        },

                        complete : function(){
                             $('.registration-form .message').html(response).delay(5000).hide('slow');
                             $('.registration-form')[0].reset();
                        },

                        success : function(data)
                        {
                            if(data.status === 1){
                                response = $('.success').addClass('show').show('slow');
                            }

                            else if(data.status === 2) {
                                response = $('.taken').addClass('show').show('slow');
                            }

                            else if(data.status === 0){
                               response =  $('.empty').addClass('show').show('slow');
                            }

                            else {
                                response = $('.error').addClass('show').show('slow');
                            }


                            $('.registration-form .message').html(response).delay(5000).hide('slow');
                            $('.registration-form')[0].reset();
                        },
                        error : function(data){
                            $('.error').addClass('show').show('slow');
                            $('.registration-form')[0].reset();
                        },

                    });

                });


            }

            /* Functions Calling */
            return {
                afterLoadThePage:function(){
                    registerForm();
                },
            }

        }(jQuery);

        /* jQuery Window Load */
        jQuery(window).on("load", function (e) {FormFunction.afterLoadThePage();});

    })(jQuery);

根据我们交易的一些评论,我设法测试了一下,发现了问题的根源。即使您将
dataType
设置为
JSON
,实际上从PHP传递的还是一个值字符串
“{\”status\”:1}”
。这是当前在
AJAX
调用的Success函数中的
data
变量的内容

在请求成功函数时添加以下代码行将完成您希望它完成的任务:
data=JSON.parse(data)。这将把PHP返回的字符串解析成JS中的JSON对象,JSON对象将创建包含所需数字类型值的
data.status
实例


我做了一些测试,它在
IF
ELSE IF
上也能正常工作。

除了
=
的用法之外,一切都很好。您必须自己检查,因为我们不知道在
success:function(data)
中返回的是什么数据,但是您的变量
data.status
可能返回数字的字符串表示形式,这是不同类型的。使用
==
比较值以及该值的数据类型。尝试放置
console.log(“data.status:type”+typeof data.status+”,value“+data.status”)
成功:函数(数据)
的开头,观察将写入控制台的内容。如果不是“number”,则所有
If
ELSE If
都将为false。如果是“number”,则值不在
If
ELSE If
之间。它表示data.status:type undefined,value undefined我实际上在做什么,从php回显json_编码,而不是重定向$feed='{“status”:1}';echo json_编码($feed);