2016-12-27 36 views
1

我正在HTTP请求的URL,其中,所述数据是变在请求的头部后,但是当我使用的$this->input->post获取它在控制器,它显示空白。在控制器没有得到angularjs后数据(笨)

Angularjs代码:

//Code to send email of quiz results 
$scope.processresult=function(){ 
    $http({ 
     method:"POST", 
     url:"http://localhost/quizci/admin/sendresultemail/", 
     data:{ 
      "riskscore":$scope.datascore 
     }, 
    }) 
    .success(function (data) { 

     if (!data.success) { 
     // if not successful, bind errors to error variables 
      $scope.errorName = data.errors.name; 
      $scope.errorSuperhero = data.errors.superheroAlias; 
     } 
     else { 
      // if successful, bind success message to message 
      $scope.message = data.message; 
     } 
    }); 
} 

控制器功能:

function sendresultemail(){ 
     $from_email = "[email protected]"; 
//   $to_email = $this->input->post('email'); 
     $to_email = "[email protected]"; 

     $this->load->model('user_model'); 
     // $result = $this->user_model->get_user_by_id($this->session->userdata($sess_array['id'])); 
     echo "risk score =".$this->input->post('riskscore'); 
     exit; 
     //Load email library 
     $this->load->library('email'); 

     $this->email->from($from_email, 'ERMS'); 
     $this->email->to($to_email); 
     $this->email->subject('Email Test'); 
     $this->email->message($_POST); 

     //Send mail 
     if($this->email->send()){ 
      $this->session->set_flashdata("email_sent","Email sent successfully.");  
     } 
     else { 
      $this->session->set_flashdata("email_sent","Error in sending Email."); 
      $this->load->view('admindash'); 

     } 

    } 

参考以下图片,在screen1

可以看到在请求有效载荷中的数据,但在响应显示为空白screen2

回答

0

您没有提及标题内容类型。

$scope.processresult=function(){ 
        $http({ 
         method:"POST", 
         url:"http://localhost/quizci/admin/sendresultemail/", 
         headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
         data:{ 
          "riskscore":$scope.datascore 
         }, 
        }) 

在PHP .. ..恩

public function store() 
{ 
    $this->load->database(); 
    $_POST = json_decode(file_get_contents('php://input'), true); 
    $posts = $this->input->post(); 
    $this->db->insert('posts', $posts); 
} 
+0

我试过前面添加标题,但它给“不允许主要人物形象。” CI中的错误,因此我删除了该错误。 – adi

+0

改变标题:{'Content-Type':'application/json'},并且php $ obj = json_decode(file_get_contents('php:// input')); –

0

你不应该被呼应它的价值在CI返回回JSON。

header('Content-Type: application/json'); 
echo json_encode($arr); 

另外,请不要使用成功,因为它是反过来使用的。

$http({ 
     method:"POST", 
     url:"http://localhost/quizci/admin/sendresultemail/", 
     data:{ 
       "riskscore":$scope.datascore 
     }, 
     }) 
     .then(function (resp) { 
       var response = rest.data; 
       // any code here 
}); 
1

有时Codeigniter无法获取发布数据。为我工作的一个解决办法是设置$ _ POST使用手动步骤:

$_POST = json_decode(file_get_contents("php://input"), true); 

然后运行$这个 - >输入 - >后()函数。

希望它可以帮助一些人。