XMLHttpRequest POST响应通过JSON总是空(Chrome扩展)
XMLHttpRequest POST response via JSON always null (Chrome Extension)
在扫描了几个线程之后,我仍然停留在这个线程上。我目前正在创建我的第一个chrome扩展,并尝试从我的REST服务获取用户信息。它看起来像这样:
private function getUserInformation(){
// Cross validation if the request method is GET else it will return "Not Acceptable" status
if($this->get_request_method() != "POST"){
$this->response('',406);
}
$userkey = $this->_request['userkey'];
$status = $this->_request['status'];
if(!empty($userkey) and !empty($status)){
$sql = mysql_query("SELECT * FROM user WHERE user_userkey = '$userkey' AND status = '$status' ", $this->db);
if(mysql_num_rows($sql) > 0){
$result = array();
while($rlt = mysql_fetch_array($sql,MYSQL_ASSOC)){
$result = $rlt;
}
// If success everythig is good send header as "OK" and return list of users in JSON format
$this->response($this->json($result), 200);
}
}
$this->response('',204); // If no records "No Content" status
}
现在我想从我的扩展发送一个用户密钥和状态,并从DB接收相应的信息。
var xhr = new XMLHttpRequest();
xhr.open("POST",'http://myHomepage/getUserInformation/',true);
xhr.onreadystatechange=function() {
if (xhr.readyState == 4) {
var res = JSON.parse(xhr.responseText || null);
console.log("res: " + res.name);
}
}
xhr.send("userkey=123thb&status=v");
这使我在控制台中始终为"null"。我遗漏了什么?
提前感谢您的帮助!
OK,终于找到错误了。为了正确POST变量,必须设置XMLHttpRequest的头信息。
下面是运行代码:
//check if userkey is in DB
var res = callUrl('POST', 'http://example/rest/getUserInformation/', 'userkey=123thb&status=v', function ausgabe(res){
// + userkey + '&status=v/', function ausgabe(res){
if (res !== null){
console.log("userkey: " + res.user_userkey);
}else{
console.log("der userkey nicht gefunden for user: " + userkey);
}
});
请注意,我现在将XMLHttpRequest放入一个函数中,如下所示:
function callUrl(type, url, parms, callback){
var xhr = new XMLHttpRequest();
xhr.open(type, url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange=function() {
if (xhr.readyState == 4) {
var res = JSON.parse(xhr.responseText || null);
callback(res);
}
}
xhr.send(parms);
}
下面是REST php代码:
private function getUserInformation(){
// Cross validation if the request method is GET else it will return "Not Acceptable" status
if($this->get_request_method() != "POST"){
$this->response('',406);
}
$userkey = $this->_request['userkey'];
$status = $this->_request['status'];
if(!empty($userkey) and !empty($status)){
$sql = mysql_query("SELECT * FROM user WHERE user_userkey = '$userkey' AND status = '$status' ", $this->db);
if(mysql_num_rows($sql) > 0){
$result = array();
while($rlt = mysql_fetch_array($sql,MYSQL_ASSOC)){
$result = $rlt;
}
// If success everythig is good send header as "OK" and return list of users in JSON format
$this->response($this->json($result), 200);
return;
}
}
$this->response('',204); // If no records "No Content" status
}
我还将这些行添加到PHP rest服务的顶部:
header('Access-Control-Allow-Origin: *');
可能不安全,但在我的情况下仅用于快速原型
相关文章:
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- 可以从Chrome扩展修改窗口对象吗
- 从popup.js|Chrome扩展访问DOM
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 在Chrome扩展内部输出Google API调用
- 检查是否存在使用chrome扩展的javascript库
- 从Chrome扩展访问Google Cloud SQL数据库
- Chrome扩展,Chrome.tabs.query的结果未定义
- 为tweet构建chrome扩展
- 在chrome扩展中使用AJAX获取目录中的文件数