【问题标题】:Ajax data not sending, but requesting fileAjax 数据不发送,但请求文件
【发布时间】:2019-06-22 05:04:33
【问题描述】:

我正在尝试使用 ajax 和 xhttprequest 构建一个聊天系统。我请求文件,文件响应,但获取数据没有到达它。我尝试使用 post 发送它们,但数据仍然没有到达 php 文件。 这是请求文件的函数:

    function refreshChat(){
  if(username != ""){

    var date = new Date();
    var timezone_offset = date.getTimezoneOffset();

    $(".messages-container").empty();

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            $(".messages-container").html(this.responseText);
       }
    };
    xhttp.open("GET", "/chat/ajax_requests/get_messages.php", true);
    xhttp.send("receiver="+username+"&sender="+chat_with+"&limit="+limit+"&timezone_offset_minutes="+timezone_offset);
  }
}

请求的文件有这个:

  $receiver = $_GET['receiver'];
  $sender = $_GET['sender'];
  $limit = $_GET['limit'];
  $timezone_offset_minutes  = $_GET['timezone_offset_minutes'];

怎么了?

【问题讨论】:

  • 您的 php 代码不打印或回显任何内容
  • 我在回应没有结果的值,但我没有写在问题中

标签: javascript php ajax get xmlhttprequest


【解决方案1】:

所以我在 w3schools 上找到了这个: send(string):将请求发送到服务器。用于 POST 请求 send():将请求发送到服务器。用于 GET 请求

我建议您将 send 函数中的字符串添加到 url,如下所示:

xhttp.open("GET", "/chat/ajax_requests/get_messages.php?"+"receiver="+username+"&sender="+chat_with+"&limit="+limit+"&timezone_offset_minutes="+timezone_offset, true);
xhttp.send();

那么您正在使用 send 函数进行获取请求。

文档链接:https://www.w3schools.com/xml/ajax_xmlhttprequest_create.asp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多