jQuery ajax提交只在第二次尝试


jQuery ajax submitting only on second try

我有一个非常奇怪的问题与jquery。我正在尝试发送一些东西到服务器并获取数据。我有成功函数和错误函数。第一次调用jquery ajax时,会调用错误函数。第二次(在同一页上),它成功了。我不知道这是为什么。下面是我的javascript代码:

$.ajax ({
    type : 'POST',
    url: "/checkout/test",
    context: document.body,
    success: function(data){
        alert (data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus)
    }
});

在服务器端,它只是一个PHP页面返回"测试"字符串,没有任何html。

这是错误发生时XMLHttpRequest的内容:

readyState: 0;
setRequestHeader: function (a, b) {
    if (!s) {
        var c = a.toLowerCase();
        a = m[c] = m[c] || a, l[a] = b;
    }
    return this;
};
getAllResponseHeaders: function () {
    return s === 2 ? n : null;
};
getResponseHeader: function (a) {
    var c;
    if (s === 2) {
        if (!o) {
            o = {};
            while ((c = bD.exec(n))) {
                o[c[1].toLowerCase()] = c[2];
            }
        }
        c = o[a.toLowerCase()];
    }
    return c === b ? null : c;
};
overrideMimeType: function (a) {
    s || (d.mimeType = a);
    return this;
};
abort: function (a) {
    a = a || "abort", p && p.abort(a), w(0, a);
    return this;
};
pipe: function (a, c) {
    return f.Deferred(function (d) {
        f.each({
            done: [a, "resolve"],
            fail: [c, "reject"]},
            function (a, c) {
                var e = c[0], g = c[1], h;
                f.isFunction(e) ? b[a](function () {
                    h = e.apply(this, arguments), h && f.isFunction(h.promise) ? h.promise().then(d.resolve, d.reject) : d[g + "With"](this === b ? d : this, [h]);
                }) : b[a](d[g]);
            });
        }).promise();
    };
    always: function () {
        return b.done.apply(b, arguments).fail.apply(this, arguments);
    };
    then: function (a, c) {
        b.done(a).fail(c);
        return this;
    };
    promise: function (a) {
        if (a == null) {
            if (d) {
                return d;
            }
            d = a = {};
        }
        var c = g.length;
        while (c--) {
            a[g[c]] = b[g[c]];
        } return a;
    };
    isRejected: function () {
        return !!c || !!b;
    };
    isResolved: function () {
        return !!c || !!b;
    };
    fail: function () {
        if (!d) {
            var c = arguments, g, h, i, j, k;
            b && (k = b, b = 0);
            for (g = 0, h = c.length; g < h; g++) {
                i = c[g], j = f.type(i), j === "array" ? e.done.apply(e, i) : j === "function" && a.push(i);
            }
            k && e.resolveWith(k[0], k[1]);
        }
        return this;
    };
    done: function () {
        if (!d) {
            var c = arguments, g, h, i, j, k;
            b && (k = b, b = 0);
            for (g = 0, h = c.length; g < h; g++) {
                i = c[g], j = f.type(i), j === "array" ? e.done.apply(e, i) : j === "function" && a.push(i);
            } k && e.resolveWith(k[0], k[1]);
        }
        return this;
    };
    success: function () {
        if (!d) {
            var c = arguments, g, h, i, j, k;
            b && (k = b, b = 0);
            for (g = 0, h = c.length; g < h; g++) {
                i = c[g], j = f.type(i), j === "array" ? e.done.apply(e, i) : j === "function" && a.push(i);
            }
            k && e.resolveWith(k[0], k[1]);
        }
        return this;
    };
    error: function () {
        if (!d) {
            var c = arguments, g, h, i, j, k;
            b && (k = b, b = 0);
            for (g = 0, h = c.length; g < h; g++) {
                i = c[g], j = f.type(i), j === "array" ? e.done.apply(e, i) : j === "function" && a.push(i);
            }
            k && e.resolveWith(k[0], k[1]);
        }
        return this;
    };
    complete: function () {
        if (!d) {
            var c = arguments, g, h, i, j, k;
            b && (k = b, b = 0);
            for (g = 0, h = c.length; g < h; g++) {
                i = c[g], j = f.type(i), j === "array" ? e.done.apply(e, i) : j === "function" && a.push(i);
            }
            k && e.resolveWith(k[0], k[1]);
        }
        return this;
    };
    statusCode: function (a) {
        if (a) {
            var b;
            if (s < 2) {
                for (b in a) {
                    j[b] = [j[b], a[b]];
                }
            } else {
                b = a[v.status], v.then(b, b);
            }
        }
        return this;
    };
    responseText: ;
    status: 0;
    statusText: error; 

谢谢你的帮助

我猜你需要为url提供文件扩展名

try this

$.post("/checkout/test", function(data){
alert(data);
});

除了jquery,你正在使用的任何其他javascript框架??查看firebug控制台是否有错误