实时加载结果(ajax)

Real-time load result (ajax)

本文关键字:ajax 结果 加载 实时      更新时间:2023-09-26

我不知道如何编写这个脚本。我需要用一些URL加载结果(例如:localhost/index.php?action=get&type=29),并且这个结果提供一个变量以供进一步处理。我需要在Jquery上创建这个。谢谢

jQuery中有一些功能可以使用,您可以使用简单的.load()函数快速加载,该函数将HTML解析为一个元素。用法:

$('#element').load('http://www.urlgoeshere.com/');

通过AJAX处理事件并提供进一步的写入选项,您可以选择许多选项:

  • http://api.jquery.com/jQuery.ajax/

以下选项使用.ajax()功能,但使编写更容易:

  • http://api.jquery.com/jQuery.get/
  • http://api.jquery.com/jQuery.post/

EDIT:通过"刷新",您可以使用setTimeout函数重复ajax调用:

setTimeout(function()
{
    $.ajax({
        type: "POST",
        url: "some.php",
        data: "action=get&type=29",
        success: function(arg){
            // do something
        }
    });
}, 1000); // This will "refresh" every 1 second
$.ajax({
    type: "POST",
    url: "some.php",
    data: "action=get&type=29",
    success: function(arg){
        // do something
    }
});

您可以使用

$.ajax({
    url: "localhost/index.php",
    data: "action=get&type=29",
    success: function(data, status, xhr){
       // data is the response from the server, status is the http status code, 
       // xhr is the actual xhr
    },
    error: function(){
    },
    ...
});

以获得结果。

请确保定义successerror回调以在返回数据时处理数据。

看这里http://api.jquery.com/jQuery.ajax/开始。