XMLHttpRequest POST响应通过JSON总是空(Chrome扩展)

XMLHttpRequest POST response via JSON always null (Chrome Extension)

本文关键字:Chrome 扩展 响应 POST JSON XMLHttpRequest      更新时间:2023-09-26

在扫描了几个线程之后,我仍然停留在这个线程上。我目前正在创建我的第一个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: *');

可能不安全,但在我的情况下仅用于快速原型