在 PHP 后端访问 XMLHttpRequests send(data)

access XMLHttpRequests send(data) in PHP backend

我有一个 XMLHttpRequest 将数据发送到 PHP 后端。

var req = new XMLHttpRequest();
req.open('GET', url);

req.onload = function() {
  // This is called even on 404 etc
  // so check the status
  if (req.status == 200) {
    // Resolve the promise with the response text
    resolve(req.response);
  }
  else {
    // Otherwise reject with the status text
    // which will hopefully be a meaningful error
    reject(Error(req.statusText));
  }
};

// Handle network errors
req.onerror = function() {
  reject(Error("Network Error"));
};

// Make the request
req.send('query=messages'); // <-- i want to access this in php

我试过了 print_r($_GET)print_r($_REQUEST) 但都不起作用。

有人知道如何访问这些数据吗?

对于 POST-requests,您只能通过 XMLHttpRequest.send() 方法发送数据,而不能通过 GET。

对于 GET-requests,您需要将数据附加到 url 作为查询字符串。

url += "?query=message";

然后您可以使用 PHP 检索数据:

$message = $_GET['query'];

更多信息:http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp