2017-09-26 121 views
1

我想在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] 
+1

有什麼錯誤? http請求是否被觸發?我們需要更多信息來排除故障 – LLai

+1

您是否會遇到'cors'錯誤?如果是的話,你必須在服務器端配置'cors'。否則需要更多關於確切錯誤的細節。 – Kuncevic

+0

請提供你有什麼錯誤。此外你的網址有2個空的空間,可能會導致問題.../coolmanz /index.php – Mehdi

回答

1

是您從$_POST變量,而您發送期待值的問題10身體是一個json對象。你的功能服務現在不會。

嘗試這個辦法:更換你的if聲明,你檢查POST數據:

if($this->input->method() === 'post') { 
    $obj = json_decode($this->input->raw_input_stream); 
    $id = $obj->id; 
    $name = $obj->name; 
    $description = $obj->description; 
    $active = $obj->active; 
    $deleted = $obj->deleted; 
    $display_order = $obj->display_order; 
    $icon = $obj->icon; 
} 
+0

試試吧,讓我們知道您的結果請: - | +1 –

+0

對你有幫助嗎? ) –

+0

是的,它解決了我的問題,運作良好 –

相關問題