Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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 Ajax上延迟的jQuery并没有将数据选项中的数组作为数组传递,而是作为字符串传递_Javascript_Jquery_Ajax_Arrays_Jquery Deferred - Fatal编程技术网

Javascript Ajax上延迟的jQuery并没有将数据选项中的数组作为数组传递,而是作为字符串传递

Javascript Ajax上延迟的jQuery并没有将数据选项中的数组作为数组传递,而是作为字符串传递,javascript,jquery,ajax,arrays,jquery-deferred,Javascript,Jquery,Ajax,Arrays,Jquery Deferred,在一个应用程序中,我想使用jquerydeliver在一个中心位置实现我们的Ajax成功和错误处理。如果我在数据选项中放入字符串,一切都会很好,但是如果我想在数据选项中放入数组,它会将其作为字符串发送 我推迟的执行: Application = { ajax: function(options) { var deferred = $.Deferred(function (d) { var defaults = { cache: false,

在一个应用程序中,我想使用jquerydeliver在一个中心位置实现我们的Ajax成功和错误处理。如果我在数据选项中放入字符串,一切都会很好,但是如果我想在数据选项中放入数组,它会将其作为字符串发送

我推迟的执行:

Application = {
ajax: function(options) {
    var deferred = $.Deferred(function (d) {
        var defaults = {
            cache: false,
            type: 'post',
            traditional: true,
            dataType: 'json'
        },
        settings = $.extend({}, defaults, options);

        d.done(settings.success);
        d.fail(settings.error);
        d.done(settings.complete);

        var jqXHRSettings = $.extend({}, settings, {
            success: function (response, textStatus, jqXHR) {
                /*
                JSON Reponse
                {
                    status: 'error' or 'success',
                    code: 200, (HTTP codes or own codes between 600 and 700 for business logic errors)
                    data: { <result> }
                }
                */
                if (settings.dataType === 'json') {
                    if (response.status == 'success') {
                        // Just resolve and give data back
                        d.resolve(response.data);
                    } else if (response.status == 'error') {
                        // Implement error handling
                        d.reject(response.data);
                    }
                } else {
                    d.resolve(response);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(jqXHR);
                d.reject(jqXHR);
            },
            complete: d.resolve
        });

        $.ajax(jqXHRSettings);
    });

    var promise = deferred.promise();
    promise.success = deferred.done;
    promise.error = deferred.fail;
    promise.complete = deferred.done;

    return promise;
}
};
结果(缺少[])

在使用上述延迟函数之前,jQuery ajax的默认实现(这很有效!):

结果(使用[])

问题是:
为什么jQuery Ajax的默认实现可以正常工作而延迟函数不能正常工作?

哦,不,我提交了这个问题,之后我在延迟函数中看到了传统选项:(我删除了它,现在它工作得很好。我可能知道它有点愚蠢;-)

所以,不要使用

var defaults = {
        cache: false,
        type: 'post',
        traditional: true,
        dataType: 'json'
    },
但是使用

var defaults = {
        cache: false,
        type: 'post',
        dataType: 'json'
    },
var defaults = {
        cache: false,
        type: 'post',
        traditional: true,
        dataType: 'json'
    },
var defaults = {
        cache: false,
        type: 'post',
        dataType: 'json'
    },