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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?