Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用RequestOptions获取角度http失败_Angular_Typescript - Fatal编程技术网

Angular 使用RequestOptions获取角度http失败

Angular 使用RequestOptions获取角度http失败,angular,typescript,Angular,Typescript,我有两个功能,一个工作,另一个给我这个404错误: /获取[object%20Object]404(未找到) 函数getNews给了我这个错误,我使用了console.log来查看这两个函数的值,我得到了相同的结果:this.newId=“1”,所以我用一个字符串调用这两个方法,使用“New”的id,但它只适用于onVote方法 为什么在getNews案例中,我在请求中得到了一个对象,而在onVote中,我有id 这是component.ts文件: export class NewDetails

我有两个功能,一个工作,另一个给我这个404错误:

/获取[object%20Object]404(未找到)

函数getNews给了我这个错误,我使用了console.log来查看这两个函数的值,我得到了相同的结果:this.newId=“1”,所以我用一个字符串调用这两个方法,使用“New”的id,但它只适用于onVote方法

为什么在getNews案例中,我在请求中得到了一个对象,而在onVote中,我有id

这是component.ts文件:

export class NewDetailsComponent implements OnInit
{
    params = new URLSearchParams();
    options = new RequestOptions({ withCredentials: true });
    oneNew: New;
    newId: string;

constructor(public http: Http, private activatedRoute: ActivatedRoute)
{    }

ngOnInit()
{
    this.activatedRoute.params.subscribe(params =>
    {
        this.newId = params['id']
    });

    this.getNews(this.newId)
}

getNews(id: string)
{
    this.params.set('id', id)
    this.options.search = this.params

    this.http
        .get(ApiConfig.API_URL + 'news/get' + this.options)
        .toPromise()
        .then(response =>
        {
            this.oneNew = <New>response.json()
        })
}

onVote(id: string)
{
    this.params.set('id', id.toString())
    this.options.search = this.params

    this.http
        .get(ApiConfig.API_URL + 'news/Vote', this.options)
        .toPromise()
}
导出类NewDetailsComponent实现OnInit
{
params=新的URLSearchParams();
options=newrequestoptions({withCredentials:true});
新的:新的;
newId:string;
构造函数(公共http:http,私有activatedRoute:activatedRoute)
{    }
恩戈尼尼特()
{
this.activatedRoute.params.subscribe(params=>
{
this.newId=params['id']
});
this.getNews(this.newId)
}
getNews(id:string)
{
this.params.set('id',id)
this.options.search=this.params
这是http
.get(ApiConfig.API_URL+'news/get'+this.options)
.toPromise()
。然后(响应=>
{
this.onew=response.json()
})
}
onVote(id:string)
{
this.params.set('id',id.toString())
this.options.search=this.params
这是http
.get(ApiConfig.API_URL+'news/Vote',this.options)
.toPromise()
}

您的get正在使用
+
而不是
添加选项:

getNews(id: string)
{
    this.params.set('id', id)
    this.options.search = this.params

    this.http
        .get(ApiConfig.API_URL + 'news/get', this.options)
        .toPromise()
        .then(response =>
        {
            this.oneNew = <New>response.json()
        })
}
getNews(id:string)
{
this.params.set('id',id)
this.options.search=this.params
这是http
.get(ApiConfig.API_URL+'news/get',this.options)
.toPromise()
。然后(响应=>
{
this.onew=response.json()
})
}

应该解决这个问题

什么时候做一个console.log的url,你有真实的url吗?我想Z.Bagley有答案。当你看到这样的错误,这意味着你有一个需要字符串的混合对象。你有
.get(ApiConfig.API_url+'news/get'+this.options)
.get(ApiConfig.API_URL+'news/Vote',this.options)
+
可能有问题。