2016-12-27 18 views
1

URLにHTTPリクエストを作成していますが、リクエストヘッダにデータがポストされていますが、コントローラで$this->input->postを使用してフェッチしていますブランク。コントローラ(codeigniter)のanglejsポストデータを取得していません

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で

.. EX ..

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 = json_decode(file_get_contents("php://input"), true); 

、その後の$ this - >入力 - >ポスト()関数を実行します。私のために働いた一つの解決策は、次のステップを使用して手動で$ _POSTを設定することでした。

一部の人々に役立つことを望みます。

関連する問題