【问题标题】:How do I send Angular 2 http.post requests to codeigniter?如何将 Angular 2 http.post 请求发送到 codeigniter?
【发布时间】:2017-06-15 22:27:18
【问题描述】:

角度代码:

sendData(obj){ 
    const userObj = JSON.stringify(obj);  console.log(userObj);
    let headers = new Headers();
    headers.append("Content Type","application/json");
    return this.http.post('http://localhost/codeigniter/index.php/ngGet_Controller/posted', userObj, headers);
}

CoeIgniter(控制器)代码:

public function posted(){
    $postdata = file_get_contents("php://input");
    var_dump(json_decode($postdata));
    print_r($this->input->post());
}

【问题讨论】:

  • 我尝试了答案,但我没有得到我的代码有什么问题,我仍然在 codeIgniter 方面得到空值,我是否需要在我的 codeIgniter 中配置一些东西?由于角度响应很好。
  • 请您也发布“userObj”。
  • 我的 userObj 给了我这个:- ({"username1":"test","email1":"test@test.com"})
  • 有人请帮忙!

标签: angular codeigniter


【解决方案1】:

请更改此行:

headers.append('Content-Type','application/json');

应该是"Content-Type" 而不是"Content Type"

【讨论】:

  • 让 headers = new Headers({ 'Content-Type': 'application/json' });但没有帮助:(
  • 在posted函数中使用$_POST再试一次
  • 尝试使用 (var_dump($_POST);) 但仍然得到空数组。
猜你喜欢
  • 2017-05-12
  • 2016-07-12
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 2015-12-30
  • 2018-05-26
  • 1970-01-01
相关资源
最近更新 更多