doutaoer3148
2017-09-26 01:59
浏览 133
已采纳

使用http.post()将数据发送到php作为服务器端

I want to send one object to backend using http.post() in Angular2,And i have used the following code for the same. I could not send data to the backend. For server side programming using codeigniter framework. I don't know whats wrong with the code. What are the changes should I make to m component.ts?

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);
});

EDIT

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条回答 默认 最新

  • duankuan5319 2017-09-26 04:25
    已采纳

    The problem is You expecting values from $_POST variable while You sending http post body the body is a json object. Your function service wouldn't now.

    TRY THIS: replace Your if statement where You check for POST data:

    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;
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题