XMLHttpRequest通过JSON的POST响应始终为null(Chrome扩展)

时间:2015-08-03 18:49:54

标签: javascript json google-chrome

扫描完几个线程后,我仍然坚持这个。 我目前正在创建我的第一个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
}

现在我想从我的分机发送用户密钥和状态,并从数据库中接收相应的信息。



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"在控制台内。 我在这里缺少什么?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,终于找到了错误。 必须设置XMLHttpRequest的头信息才能正确地POST变量。

以下是正在运行的代码:



//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休息服务的顶部:



  header('Access-Control-Allow-Origin: *');




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