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

使用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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题