问题
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