0

chriskacerguis/codeigniter-restserverを使用して、Codeigniter で単純な REST API を作成しています。ログイン メソッドを正常に作成し、postman を使用してテストしましたが、動作しますが、Jquery AJAX を使用して API を使用する単純なログイン ページを作成しましたが、それを介して送信しようとすると、$this->post()読み取れませんが、郵便配達員で動作します。

コントローラ

<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

use Restserver\Libraries\REST_Controller;

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");

    class Api extends REST_Controller
    {


        public function user_login_post()
        {
            $req = $this->post(); // always empty

            if (empty($req)) {
                $status = parent::HTTP_EXPECTATION_FAILED;
                $response = ['status' => $status, 'msg' => 'Missing data!'];

                $this->response($response, $status);

                exit();
            }

            $user = $this->User_model->get($req['email']);

            if (empty($user)) {
                $status = parent::HTTP_NOT_FOUND;
                $response = ['status' => $status, 'msg' => 'Email does not exist!'];

                $this->response($response, $status);

                exit();
            }

            if (md5($req['password']) != $user->password) {
                $status = parent::HTTP_FORBIDDEN;
                $response = ['status' => $status, 'msg' => 'Invalid email or password!'];

                $this->response($response, $status);

                exit();
            }

            // Generate token
            $tokenData['id'] = $user->user_id;
            $tokenData['email'] = $user->email;
            $tokenData['type'] = 'user';

            $token = AUTHORIZATION::generateToken($tokenData);

            $status = parent::HTTP_OK;

            $response = ['status' => $status, 'token' => $token, 'logged_in' => true];

            $this->response($response, $status);

        }
    }

上記のメソッドにリクエストを送信するために使用したJSコードは次のとおりです

$('#submit').click(function (e) {
    e.preventDefault();

    const url = "<?= base_url() ?>" + 'api/user_login';

    $.ajax({
        type: 'POST',
        url: url,
        data: {
            "email": "test@gmail.com",
            "password": "password"
        },
        dataType: 'json',
        cache : false,
        processData: false,
        success: function (res) {
            console.log(res);

        },
        error: function (err) {
            console.log(err);

        }
    })

});

サーバー側でデータを取得できません。どんな助けでも大歓迎です。

4

0 に答える 0