我正在向网址发出HTTP请求,其中数据在请求标头中发布,但是当我使用$this->input->post
在控制器中获取数据时,它显示为空白。
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 = "test@test.com";
// $to_email = $this->input->post('email');
$to_email = "abc@gmail.com";
$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');
}
}
中
答案 0 :(得分:2)
有时,codeigniter无法获取发布数据。一个对我有用的解决方案是使用以下步骤手动设置$ _POST:
$_POST = json_decode(file_get_contents("php://input"), true);
然后运行$ this-> input-> post()函数。
希望它可以帮助一些人。
答案 1 :(得分: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);
}
答案 2 :(得分: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
});