如何将Angular 2 http.post请求发送给codeigniter?

问题描述:

角代码:如何将Angular 2 http.post请求发送给codeigniter?

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()); 
} 
+0

试试这个答案https://*.com/questions/41154319/how-to-post-json-object-with-http-post-angular-2-php-server-side/41155043#41155043 –

+0

我试过答案,但没有得到什么错误在我的代码,即时通讯仍然在codeIgniter一侧,应该我需要配置我的codeIgniter的东西吗?由于角度响应很好。 – James

+0

你也可以发布“userObj”。 –

请改变这一行:

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

应该"Content-Type""Content Type"

+0

试过这个: - – James

+0

let headers = new Headers({'Content-Type':'application/json'});但没有帮助:( – James

+0

使用$ _POST里面发布的函数,并再次尝试 –