简体   繁体   English

Codeigniter,香草JS:发送Ajax发布请求,但未接收到php中的任何数据

[英]Codeigniter, Vanilla JS: Sending Ajax post request but not recieving any data in php

Good Day Everyone I have a little problem with sending and recieving post data with ajax. 大家好,我在使用ajax发送和接收帖子数据时遇到一些问题。 I'm not new to this matter. 我对这件事并不陌生。 I just recently started using the codeigniter framework, and now, the pattern I have always used for ajax doesn't work anymore. 我刚刚开始使用codeigniter框架,现在,我一直用于ajax的模式不再起作用。 (Never used a 'foreign' framework before, but my own) I hope you can help me. (以前从未使用过“外国”框架,但我自己使用过)我希望您能为我提供帮助。

The main problem is, that the php controller doesn't recieve the post data. 主要问题是php控制器无法接收发布数据。

ajax.js (ajax handling, AJAX_URL is ' http://localhost/ajax/process ' at the moment of th call, I already checked that): ajax.js (ajax处理,在调用时AJAX_URL是' http:// localhost / ajax / process ,我已经检查过了):

function AjaxRequest(callback, data) {
   this.data     = (data!== undefined) ? data : [];
   this.callback = callback;
   this.request  = new XMLHttpRequest();

   this.addData = function(key, value) {
      this.data[key] = value;
   };

   this.send = function() {
      var callback = this.callback;
      this.request.open('POST', <b>AJAX_URL</b>, true);
      this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.request.onreadystatechange = function() {
         if (this.readyState === 4 && this.status === 200) {
            callback(this.responseXML);
         }
      };

      var data = this.createPostData();
      this.request.send(data); 
   };

   this.createPostData = function() {
      var data = '';
      for (var key in this.data) {
         data += key + '=' + this.data[key] + '&';
      }
      data = data.slice(0, -1);
      console.log(data);
   };
}

actions.js (contains action handling from website): actions.js (包含来自网站的操作处理):

function requestLogin() {
   var username = $("input#txtUsername").val();
   var password = $("input#txtPassword").val();

   var request = new AjaxRequest(callbackLogin);
   request.addData('action'  , 'login_user');
   request.addData('username', username);
   request.addData('password', password);
   request.send();
}

function callbackLogin(xml) {
   console.log(xml);
}

Ajax.php (CI_Controller for ajax request handling): Ajax.php (用于处理ajax请求的CI_Controller):

class Ajax extends CI_Controller {
   public function __construct() {
      parent::__construct();  

      $this->load->library('session');
      $this->load->model('user_model');
   }

   public function process() {
      $data = $this->input->post();
      $action = $data['action'];
      if (method_exists($this, $action)) {
         $this->$action($data);
      }
   }

   public function login_user($data) {
      $passwd_enc = md5($data['password']);
      $xml_resp   = new Xml_response();

      $user = $this->user_model->login_user($data['username'], $passwd_enc);
      $xml_resp->start();
      if (!$user) {
         $xml_resp->error();
         $xml_resp->send_message(Xml_message::ERROR, 'Benutzername oder Kennwort ist inkorrekt.');
      } else {
         $xml_resp->success();
         $xml_resp->send_message(Xml_message::SUCCESS, 'Sie wurden erfolgreich angemeldet.');
         $xml_resp->send_data('user', $user);
      }
      $xml_resp->end();
   }
}

Now when I try to send the request, the method process is called (checked through logging) but the post array is empty (in each possibility: $_POST or codeigniter's own post array) and I recieve a responseXML which is null . 现在,当我尝试发送请求时,方法过程被调用(通过日志记录进行检查),但是post数组为空(每种情况:$ _POST或codeigniter自己的post数组),并且我收到一个responseXML,它为null

Is there anything I have done wrong in this part or do I have to consider something else when using codeigniter? 我在这部分做错了什么吗?使用Codeigniter时是否还需要考虑其他事项?

Thank you very much in advance and please don't be shocked by my bad english (I come from Switzerland ;) 预先非常感谢您,请不要因为我的英语不好而感到震惊(我来自瑞士;)

Best Regards, Tobias Widner 最好的问候,Tobias Widner

Try adding a slash at the end of your request URL. 尝试在请求网址的末尾添加斜杠。 I don't know if your codeigniter configuration is the same as mine but if I don't put a slash at the end of the URL, my .htaccess redirects to the URL with a slash on. 我不知道您的codeigniter配置是否与我的相同,但是如果我在URL末尾不加斜杠,则我的.htaccess重定向到带有斜杠的URL。 This redirect removes any post data. 此重定向将删除所有发布数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM