doujiaozhao2489 2012-08-23 03:30
浏览 23
已采纳

用http代码返回重复的AJAX调用

When a button is clicked I have a jquery AJAX call such as:

$(".play").click(function(){
    function doAjax(){
        $.ajax({
        url: 'blah.php',
        success: function(data) {
            if (data==250)
            {
                setTimeout(doAjax, 2000);
            }
            else
            {
                $('#quote p').html(data);
            }

        }
        });
    }
    doAjax();
});

Any data from the server is printed to the div:

<div id="quote"><p> </p></div>

blah.php on the server does some processing to retrieve some status codes:

$r = new HttpRequest('http://localhost...', HttpRequest::METH_GET);
$r->send();

if ($r->getResponseCode() == 200)
{
echo "Starting video..."        
//sends a video to play
}
else if ($r->getResponseCode() == 450)
{
    echo "Oops! Not found...";
}
else if ($r->getResponseCode() == 550)
{
    echo "Oops! An error occurred...";
}
else if ($r->getResponseCode() == 250)
{
    echo "Initializing...";
}
else
{
    echo "Now we are in trouble";
}

The response codes ($r->getResponseCode()) get returned as I expect, so my problem is with my repeated ajax call with setTimeout. What I want to happen is that when code 250 is returned it prints out Initializing, which it does, but then my AJAX call will repeat because the success function is called with 250. This doesn't work. It says Initializing but never repeats, because the next step is that I should see Starting video....

Everything else seems to check out, I guess the problem is with my jquery success function?

  • 写回答

3条回答 默认 最新

  • dq_1984 2012-08-23 03:49
    关注

    The problem here is that the "data" variable that you have access to in Javascript is actually the exact text that you're echoing in your PHP file, blah.php.

    So a more accurate way of making it work is checking if data == "Initializing...". You may also want to consider outputting JSON-compatible text from your PHP file so that you can get both a status code and a message.

    Consider putting this as your PHP:

    $code = $r->getResponseCode();
    $message = "";
    
    switch($code) {
        case 200:
            $message = "Starting video...";
            break;
        case 450:
            $message = "Oops! Not found...";
            break;
        case 550:
            $message = "Oops! An error occurred...";
            break;
        case 250:
            $message = "Initializing...";
            break;
        default:
            $message = "Unknown status";
            break;  
    }
    
    print "{ 'code': $code, 'message': '$message' }";
    

    The above will output all the data you need, in JSON format.

    In your javascript, consider using jQuery's built in getJSON method, and parse the data as a regular javascript object.

    $.getJSON('blah.php', function(data) {
            if (data['code'] == 250)
            {
                setTimeout(doAjax, 2000);
            }
            else
            {
                $('#quote p').html(data['message']);
            }
    
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测