Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在PHP后端访问XMLHttpRequests发送(数据)_Javascript_Php_Ajax_Http Headers - Fatal编程技术网

Javascript 在PHP后端访问XMLHttpRequests发送(数据)

Javascript 在PHP后端访问XMLHttpRequests发送(数据),javascript,php,ajax,http-headers,Javascript,Php,Ajax,Http Headers,我有一个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

我有一个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
var-req=new-XMLHttpRequest();
请求打开('GET',url);
req.onload=函数(){
//这甚至在404上也被称为
//所以检查一下状态
如果(请求状态==200){
//用响应文本解析承诺
解决(请求响应);
}
否则{
//否则,拒绝状态文本
//这将有望成为一个有意义的错误
拒绝(错误(请求状态文本));
}
};
//处理网络错误
req.onerror=函数(){
拒绝(错误(“网络错误”);
};
//提出请求

请求发送('query=messages');// 对于POST请求,您只能通过
XMLHttpRequest.send()
-方法发送数据,而不是GET

对于GET请求,需要将数据作为查询字符串附加到url

url += "?query=message";
然后,您可以使用PHP检索数据:

$message = $_GET['query'];

更多信息:

您只能通过
XMLHttpRequest.send()
-方法发送数据,用于POST请求,而不是GET请求

对于GET请求,需要将数据作为查询字符串附加到url

url += "?query=message";
然后,您可以使用PHP检索数据:

$message = $_GET['query'];

更多信息:

您是否在浏览器控制台中观看了请求/响应?是@JayBlanchard它似乎根本没有传递query=消息:GET/rest/api.php HTTP/1.1主机:本地主机连接:保持活动来源:用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/47.0.2526.111 Safari/537.36接受:/Referer:Accept编码:gzip,deflate,sdch接受语言:de-de,de;q=0.8,在美国;q=0.6,en;q=0.4请看下面我的答案为什么…@MagnusEriksson是的,非常感谢!在阅读您的回答之前,我回答了您的评论您是否在浏览器控制台中观看了请求/响应?是的@JayBlanchard它似乎根本没有传递query=message:GET/rest/api.php HTTP/1.1主机:本地主机连接:keep alive来源:用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/47.0.2526.111 Safari/537.36接受:/Referer:Accept编码:gzip,deflate,sdch接受语言:de-de,de;q=0.8,在美国;q=0.6,en;q=0.4请看下面我的答案为什么…@MagnusEriksson是的,非常感谢!我在读你的回答之前回答了你的评论啊,好吧,我不知道。使用XMLHttpRequest调用PHP REST API似乎有点不方便,因为我必须使用URL来获取和发送POST。香草js有更好的解决方法吗?不太好。我会为它做一个小包装,并像这样使用:
ajax.get(url、数据、回调)
ajax.post(url、数据、回调)
并用这些方法处理请求。啊,好吧,我不知道。使用XMLHttpRequest调用PHP REST API似乎有点不方便,因为我必须使用URL来获取和发送POST。香草js有更好的解决方法吗?不太好。我会为它做一个小包装,并像这样使用:
ajax.get(url、数据、回调)
ajax.post(url、数据、回调)
并用这些方法处理请求。