小编lit*_*ian的帖子

在同步Ajax上加载指示符

我在我的网站上使用ajax和jQuery,需要显示进度/加载指示器.

我的困境是这样的:

  • 同步AJAX锁定浏览器,所以我无法做任何事情(例如显示加载指示器)直到内容被返回,到那时为时已晚
  • 我使用JSON作为返回数据类型,并设置async = true返回一个空响应字符串
  • 我的整个框架依赖于JSON格式的返回类型

我似乎找不到任何方法让JS给用户一个正在进行的指示,除了做一个alert().(由于某种原因,警报确实有效).

有什么建议?

我的代码:

JS

var jqXHR = $.ajax(
{
    type: "POST",
    url: url, 
cache: false,
    data: params, // array of parameters
    async: false, // responseText is empty if set to true
    dataType: 'json',
    error: function()
    {
        alert("Ajax post request to the following script failed: " + url);
    }

} ); 

var html = jqXHR.responseText;
Run Code Online (Sandbox Code Playgroud)

PHP

$return = array();
$return['status'] = 1;
$return['message'] = "some html here ...";
echo json_encode($return);
Run Code Online (Sandbox Code Playgroud)

ajax jquery json loading synchronous

9
推荐指数
2
解决办法
3万
查看次数

标签 统计

ajax ×1

jquery ×1

json ×1

loading ×1

synchronous ×1