jQuery ajax请求只能同步工作,异步时不返回

时间:2012-09-19 21:08:19

标签: jquery asynchronous

我通过jQuery ajax向.NET通用处理程序(.ashx)发出请求。

我正在返回JSON,我已在JSONLint上验证过,但由于它是敏感数据而无法发布。

问题是调用仅在同步完成时返回(async:false)。当异步完成时,它永远不会回来。 async:false将调用我的成功函数以及所有全局函数。 async:true只会调用全局函数ajaxStartajaxSendasync:false会调用这些函数以及其他函数。

我不知道如何进一步调试或确定没有回复的原因。

$.ajax({
    type: "POST",
    async: false,
    cache: false,
    timout: 600000,
    data: request_object.GetQueryString(),
    dataType: "json",
    responseType: "json",
    error: function (request, error_type, error_message) {
        alert("error");
    },
    success: function (data, status, request) {
        alert("success");
    },
    url: "./ReportRequestHandler.ashx",
    complete: function () {
        alert("here");
    }
});

$(document).ajaxError(function () {
    alert("ajaxError");
}).ajaxSend(function () {
    alert("ajaxSend");
}).ajaxStart(function () {
    alert("ajaxStart");
}).ajaxStop(function () {
    alert("ajaxStop");
}).ajaxSuccess(function () {
    alert("ajaxSuccess");
}).ajaxComplete(function () {
    alert("ajaxComplete");
});

3 个答案:

答案 0 :(得分:0)

我认为这是因为“。”在URL中。你尝试删除了吗?检查这个也有类似的东西

jQuery not getting reply when using asynchronous

希望有所帮助

答案 1 :(得分:0)

我在你的配置没有async属性的页面中运行代码,它似乎对我来说很好..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Pages" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

    <title></title>

    <script type="text/javascript">
        $(document).ajaxError(function() {
            alert("ajaxError");
        }).ajaxSend(function() {
            alert("ajaxSend");
        }).ajaxStart(function() {
            alert("ajaxStart");
        }).ajaxStop(function() {
            alert("ajaxStop");
        }).ajaxSuccess(function() {
            alert("ajaxSuccess");
        }).ajaxComplete(function() {
            alert("ajaxComplete");
        });
        $.ajax({
            type: "POST",
            timout: 600000,
            data: { 'type': 'check' },
            dataType: "json",
            responseType: "json",
            error: function(request, error_type, error_message) {
                alert("error");
            },
            success: function(data, status, request) {
                alert("success");
            },
            url: "../../Ajax/GenericData.ashx",
            complete: function() {
                alert("here");
            }
        });

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
    </div>
    </form>
</body>
</html>

我按顺序收到了所有警报消息。你能查看浏览器的控制台部分,看看当你的配置中删除了async:false时,你的请求是怎么回事。

答案 2 :(得分:0)

我发现了问题。 用于触发事件的按钮是提交页面并重新加载太快,我看不到任何闪烁。

相关问题