如何在角度2中设置标题

时间:2018-02-01 06:40:42

标签: angular api http-headers response mean

我有以下标题

var headers = new HttpHeaders();            
    headers.set('Content-Type', 'application/json');
    headers.set('X-Quikr-App-Id', '1087');
    headers.set('X-Quikr-Token-Id', '785582933');
    headers.set('X-Quikr-Signature-v2', '86742d91e19bc30cdd923bf6a84194c133570659');
    headers.set('Access-Control-Allow-Origin', '*');


this.http.get('https://api.quikr.com/public/adsByLocation?lat=28.64649963&lon=77.22570038&from=0&size=1', {
                     headers:headers })
  .subscribe(

这是一个例外,而当我从Postman那里做同样的事情时,它正在发挥作用。这里奇怪的是,当我从我的应用程序运行此操作时,我只在选项中出现错误,即即使调用了get调用,我也无法在响应中看到任何标题。
任何人都可以建议我帮忙。谢谢。

  Request URL:https://api.quikr.com/public/adsByLocation?lat=28.64649963&lon=77.22570038&from=0&size=1
Request Method:GET
Status Code:401 Unauthorized
Remote Address:104.120.157.164:443
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Connection:keep-alive
Content-Length:12
Content-Type:text/plain; charset=utf-8
Date:Thu, 01 Feb 2018 07:20:20 GMT
WWW-Authenticate:xBasic realm="fake"
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:api.quikr.com
Origin:http://localhost:4200
Pragma:no-cache
Referer:http://localhost:4200/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36

2 个答案:

答案 0 :(得分:1)

不要将该套装分开,只需

$(".stayalert_checkbox").on('click',function(){
  if($(this).prop("checked",false)){
    $(this).prop("checked",true)
    $("#stay_alert_div").toggle();
  }else{
    $(this).prop("checked",false)
    $("#stay_alert_div").toggle();
  }
});

HttpHeaders是" inmutable",其他方法

var headers = new HttpHeaders()
    .set('Content-Type', 'application/json');
    .set('X-Quikr-App-Id', '1087');
    .set('X-Quikr-Token-Id', '785582933');
    .set('X-Quikr-Signature-v2', '86742d91e19bc30cdd923bf6a84194c133570659');
    .set('Access-Control-Allow-Origin', '*');

答案 1 :(得分:0)

引用Angular 2 Angular Http Guide @ angular / http已被弃用,@ angular / common / http应该是你在Angular 2 App中使用的那个。因为如果您没有指定http标头,则默认请求将作为Content-Type text / plain发送,如何修改http标头是:

import { HttpClient, HttpHeaders } from '@angular/common/http';  
.....
const req = this.http.get('https://api.quikr.com/public/adsByLocation?lat=28.64649963&lon=77.22570038&from=0&size=1',
                        JSON.stringify(object),
                        {
                         headers:new HttpHeaders()
                         .set('Content-Type','application/json' 
//other headers here
                         }).subscribe();