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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作