尝试重新访问javascrip中的URL

2024-09-30 08:24:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想去参观某个休息区。你知道吗

$.ajax({
    type: "POST",
    url: url + "result/" + ticket_id,
    success: function(data) {
        setTimeout(function(){pollResponse(url,data.id);}, 3000);
    }
});

这很管用。它访问URI,views函数完成它的工作。因此,继续success它将转到新的URI。你知道吗

function pollResponse(url, id)
{
    $.getJSON(url + "status/" + id, {},
        function(data) {
            if (data.report == null)
            {
                console.log(data.status_response);
                setTimeout(function(){pollResponse(url, id);}, 3000);
            }
            else
                console.alert('DONE!');
        }
    );
};

如果URI没有返回report作为数据的一部分(或者为空),那么我们将在3秒钟后再次询问。你知道吗

但是在控制台中打印日志之后,响应将以[200] OK停止。 它不再询问服务器。你知道吗

我对Python的views函数所做的就是

from cornice import Service
status = Service(name='status',
                    path=root+'/status/{some_id}',
                    description=status_desc)
@status.get()
def get_status(request):
   // do something

   if (...):
      return {'report': ''}
   else:
      return {'report': 'NOT EMPTY')

Tags: 函数reportidurldataifstatusfunction
2条回答

结果中可能存在解析问题或跨域问题。尝试更改代码以直接使用ajax函数:

$.ajax({
    url: url + "status/" + id,
    dataType: 'json',
    data: {},
    success: function(data) {
        if (data.report == null)
        {
            console.log(data.status_response);
            setTimeout(function(){pollResponse(url, id);}, 3000);
        }
        else
            console.alert('DONE!');
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.error(textStatus);
        console.error(errorThrown);
        console.log(jqXHR.responseText);
    }
);

这样您就可以看到响应是否导致了错误。如果有解析错误,您可以通过jqXHR.responseText文件关于误差函数,看看有什么问题。你知道吗

jQuery中AJAX的快捷方式函数在很多情况下都会自动出错。这就是为什么我很少使用它们。你知道吗

看起来轮询函数中的setTimeout应该是:

setTimeout(function(){pollResponse(url, data.id);}, 3000);

而不是:

setTimeout(function(){pollResponse(url, id);}, 3000);

相关问题 更多 >

    热门问题