Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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 jQuery$.ajax()调用挂起,在调用结束之前,我无法执行任何操作_Javascript_Jquery_Asp.net_Ajax_Webmethod - Fatal编程技术网

Javascript jQuery$.ajax()调用挂起,在调用结束之前,我无法执行任何操作

Javascript jQuery$.ajax()调用挂起,在调用结束之前,我无法执行任何操作,javascript,jquery,asp.net,ajax,webmethod,Javascript,Jquery,Asp.net,Ajax,Webmethod,嗨 当我使用fromjQuery ajax时,页面会一直冻结到请求结束。 这是我的JavaScript代码: function GetAboutContent(ID, from) { var About = null; if (from != true) from = false; $.ajax({ type: "POST", url: "./ContentLoader.asmx/GetAboutContent", contentType: "applicat

当我使用from
jQuery ajax
时,页面会一直冻结到请求结束。 这是我的JavaScript代码:

function GetAboutContent(ID, from) {
var About = null;

if (from != true)
    from = false;

$.ajax({
    type: "POST",
    url: "./ContentLoader.asmx/GetAboutContent",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify({ 'ID': ID, 'from': from }),
    async: true,
    success: function (msg) {
        var Result = msg.d.Result;

        if (Result == 'session') {
            warning('Your session has expired, please login again!');
            setTimeout(function () {
                window.location.href = "Login.aspx";
            }, 4000);
            return;
        }

        if (Result == 'failed' || Result == false) {
            About = false;
            return;
        }

        About = JSON.parse(msg.d.About)[0];
    }
});

return About;
}

这是我的
WebService

[WebMethod(EnableSession = true)]
public object GetAboutContent(int ID, bool from = false)
{
    try
    {
        if (HttpContext.Current.Session["MahdParent"] != null ||
            HttpContext.Current.Session["MahdTeacher"] != null ||
            from)
        {
            functions = new GlobalFunctions();
            DataTable queryResult = new DataTable();

            queryResult = functions.DoReaderTextCommand("SELECT Field FROM TT WHERE  ID = " + ID);
            if (queryResult.Rows.Count != 0)
                return new { Result = true, About = JsonConvert.SerializeObject(queryResult.Rows[0].Table) };
            else
                return new { Result = false };
        }
        else
            return new { Result = "session" };
    }
    catch (Exception ex)
    {
        return new { Result = "failed", Message = ex.Message };
    }
}
我怎样才能解决这个问题?
请帮助我

在您尝试返回的最后一行
关于
。由于AJAX请求的异步性质,这无法工作。当AJAX请求的success函数运行时,声明
returnabout
的点不再存在

我想你会尝试做这样的事情:

$('div#content').html(GetAboutContent());
$.ajax({
    type: "post",
    url: "./ContentLoader.asmx/GetAboutContent",
    dataType: "json",
    data: { 
      'ID': ID, 
      'from': from 
    },
    success: function(result) {
        $('div#content').html(result)
    }
});
在PHP或Perl这样的程序语言中,这很好,但JavaScript的功能却截然不同。要制作这样的作品,你需要这样做:

$('div#content').html(GetAboutContent());
$.ajax({
    type: "post",
    url: "./ContentLoader.asmx/GetAboutContent",
    dataType: "json",
    data: { 
      'ID': ID, 
      'from': from 
    },
    success: function(result) {
        $('div#content').html(result)
    }
});
两位代码之间的区别在于,第一位代码希望JavaScript在您请求时知道该值。但是,函数
GetAboutContent()
不能立即访问这些数据。相反,它会触发一个AJAX请求,然后立即结束,请求仍在进行中,时间未知


一旦请求成功完成,JavaScript就可以使用数据。您可以在为
success
定义的回调函数中使用它。到那时,请求与启动它的函数完全没有连接。这就是为什么它是异步的。

您不能从异步函数返回结果可能与的重复您的意思是我应该将
async
设置为
false
否,我不是这个意思,您在任何情况下都不应该这样做,请阅读上面的链接。您不需要JSON.stringify,您可以在那里传递对象。如果您有一个数据类型,您也不需要JSON.parse,但不确定您在其中包含了什么。