in angular get(), how to send a value in header,

流过昼夜 提交于 2020-05-17 06:15:27

问题


My Angular code is as follows

getProgramServices(program_id)
  {
    //environment.dev_url_prefix
    console.log(program_id);
    let header = new HttpHeaders();
    // let headers = new HttpHeaders().append('key', 'value');

    // header.append('program_id', program_id);
    // header.set('program_id', program_id);
    return this.http.get('http://localhost/workers/programServiceList',
    {headers: new HttpHeaders().set('program_id', program_id ) }).pipe(map(result => {
      console.log(result);
    return result;

    }));
  }

and rest api in codeigniter is as follows:

public function programServiceList_get()
{
    header("Access-Control-Allow-Origin: *");
    $method = $_SERVER['REQUEST_METHOD'];
    if($method != 'GET' && !($this->input->get_request_header('program_id', TRUE))){

        json_output(400,array('status' => 400,'message' => 'Bad request! Program ID Not Present')); 
    }
    else {
                $resp = $this->Sponsor_api_model->programServiceList();
                $response = 200;
                json_output($response,$resp);
    }
}

Every time it shows 'Bad request! Program ID Not Present'


回答1:


Have you tried

const headers = new HttpHeaders({
    'program_id': program_id
});

this.http.get('<url', { headers }).pipe(
    map(result => {
        console.log(result);
        return result;
    })
);



回答2:


getProgramServices(program_id) {
    const _headers = new HttpHeaders({ 'program_id': program_id}` });
    const _url = 'http://localhost/workers/programServiceList';

    return this.http.get(_url, {
        headers: _headers
    });
}

Simple as that.



来源:https://stackoverflow.com/questions/61514923/in-angular-get-how-to-send-a-value-in-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!