我想在Angular2中使用http.post()發送一個對象到後端,並且我已經使用了以下代碼。我無法將數據發送到後端。對於使用codeigniter框架的服務器端編程。我不知道代碼有什麼問題。我應該對m個component.ts做些什麼改變?使用http.post()發送數據到php作爲服務器端
component.ts
let headers = new Headers();
headers.append('Content-Type', 'application/json; charset=UTF-8');
let addcategory_body={
'name':this.add_ser_name,
'description':this.add_ser_name,
'icon':'asfsdfdf.png'
}
this.http.post('http://www.viarchtechnologies.com/projects/coolmanz/index.php/ws/service', addcategory_body, {
headers: headers
}).subscribe(res => {
console.log('post result %o', res);
});
編輯
ws.php
public function service() {
$this->load->model('Coolmodel','m',true);
$retval = array();
$icon = '';
if($_POST) {
$id = $this->input->post('id');
$name = $this->input->post('name');
$description = $this->input->post('description');
$active = $this->input->post('active');
$deleted = $this->input->post('deleted');
$display_order = $this->input->post('display_order');
$icon = $this->input->post('icon');
if($name == '') {
$retval['status'] = '0';
$retval['error'] = 'name missing';
} else if($description == '') {
$retval['status'] = '0';
$retval['error'] = 'description missing';
} else {
if($active == '') {
$active = 1;
}
if($deleted == '') {
$deleted = 0;
}
if($display_order == '') {
$display_order = $this->m->get_nextOrder_Service();
}
$data = array(
'name' => $name,
'active' => $active,
'icon' => $icon,
'description' => $description,
'deleted' => $deleted,
'display_order' => $display_order
);
$retval['status'] = '1';
if($id == '') {
$this->m->saveToDB('services', $data);
} else {
$data['modified'] = date('Y-m-d H:i:s');
$this->m->updateDB('services', $data, 'id', $id);
}
//$user=$this->m->login($userlogin, md5($password));
}
} else {
$services = $this->m->get_services();
$retServices = array();
$i = 0;
foreach($services as $service) {
if($service['icon'] != '') {
$service['icon'] = base_url().'images/'.$service['icon'];
}
$retServices[$i++] = $service;
}
$retval['status'] = '1';
$retval['services'] = $retServices;
}
header('Content-Type: application/json');
echo json_encode($retval, JSON_UNESCAPED_UNICODE);
}
[![Respose][1]][1]
有什麼錯誤? http請求是否被觸發?我們需要更多信息來排除故障 – LLai
您是否會遇到'cors'錯誤?如果是的話,你必須在服務器端配置'cors'。否則需要更多關於確切錯誤的細節。 – Kuncevic
請提供你有什麼錯誤。此外你的網址有2個空的空間,可能會導致問題.../coolmanz /index.php – Mehdi