CodeIgniter - How to return Json response from controller

后端 未结 4 1231
梦谈多话
梦谈多话 2020-12-05 10:15

How do I return response from the controller back to the Jquery Javascript?

Javascript

$(\'.signinform\').submit(function() { 
   $(this).ajaxSubmit(         


        
4条回答
  •  余生分开走
    2020-12-05 10:53

    //do the edit in your javascript
    
    $('.signinform').submit(function() { 
       $(this).ajaxSubmit({ 
           type : "POST",
           //set the data type
           dataType:'json',
           url: 'index.php/user/signin', // target element(s) to be updated with server response 
           cache : false,
           //check this in Firefox browser
           success : function(response){ console.log(response); alert(response)},
           error: onFailRegistered
       });        
       return false; 
    }); 
    
    
    //controller function
    
    public function signin() {
        $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);    
    
       //add the header here
        header('Content-Type: application/json');
        echo json_encode( $arr );
    }
    

提交回复
热议问题