doutizong8099 2019-04-28 06:33
浏览 66
已采纳

POST无法在CodeIgniter中工作,而GET工作正常

I'm facing this issue with POST in CodeIgniter which is not working while if I switch to GET, that works fine.

Login Controller

public function login_check(){
    print_r($this->input->post());
    if($this->input->post('email')!=NULL){
        echo '1';
    }
    else{
        header('Content-Type: application/json');
        echo json_encode( array('a' => $this->input->post('email')));
}

CSRF is set to false in config file, while base url is set to http://localhost/xyz/

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Routes

$route['api/login-check'] = 'login/login_check';

If I set $this->input->get('email') while setting method GET in postman, that works absolutely okay.

What's something that I'm missing? Any help in this will be appreciated.

EDIT:

Response from postman:

Array() {"a":null}
  • 写回答

1条回答 默认 最新

  • doutao4480 2019-04-28 09:35
    关注

    The code is doing exactly as you are asking it to do.

    A break down of your code goes like...

    If I get something from $this->input->post('email') then 
         echo '1';
    else if $this->input->post('email') is NULL
         then assign NULL to a and return it in a json_encoded array.
    

    Going by your code it might meant to be something like...

    public function login_check(){
        print_r($this->input->post());
        if($this->input->post('email') == NULL){ // This was != 
            echo '1';
        }
        else{
            header('Content-Type: application/json');
            echo json_encode( array('a' => $this->input->post('email')));
    }
    

    The Only change being to alter != to == in your if statement.

    One of those "stare at it too long and never see it" simple Logic Error :)

    A better recommendation is to use something like...

    public function login_check(){
        print_r($this->input->post());
    
        if($this->input->post('email')){
            header('Content-Type: application/json');
            echo json_encode( array('a' => $this->input->post('email')));
            exit();
        }
        else {
            // Handle the case where no email was entered.
            echo '1';
        }
    }
    

    That should get you back on track.

    Update: I have tried this in postman (just installed it ) and for a POST you need to set the key/value under Body and not Headers as you do with GET, if that's something you were also missing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行