gpt4 book ai didi

Angular 2 Http在主体上获取返回类型3错误

转载 作者:可可西里 更新时间:2023-11-01 17:23:09 24 4
gpt4 key购买 nike

当我尝试从 asp.core api 返回响应时遇到问题“angular 2 http get Object { _body: error, status: 0, ok: false, statusText: “”, headers: {...}, type: 3, url: null }”

组件:

import { Component, OnInit } from '@angular/core';
import { IIndustryType } from '../../../model/IndustryType';
import { DeveloperJobDataService }from'../../../services/developerjob.dataservice';

@Component({
selector: 'add-job',
templateUrl: './addJob.component.html',
styleUrls: ['./addJob.component.css'],
providers: [DeveloperJobDataService]
})
export class AddJobComponent {
errorMessage: string;
private industry: IIndustryType[];

constructor(private dataService: DeveloperJobDataService) {
this.dataService.GetIndustries().subscribe(result => {
this.industry = result.json() as IIndustryType[];
console.log(this.industry);
}, error => console.error(error));
}
}

接口(interface):

export interface IIndustryType {
id: number,
name: string,
description: string,
isActive: boolean,
createdDate: Date
}

服务:

import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions } from '@angular/http';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import { IIndustryType } from '../model/IndustryType';
import { IndustryType } from '../model/viewModel';

@Injectable()

export class DeveloperJobDataService {
private baseUrl: string = 'http://localhost:49861/api/Industries';
constructor(private http: Http) { }

public GetIndustries() {
return this.http.get(this.baseUrl);
}
}

错误:

enter image description here

最佳答案

你错过了 requestOptions 中的 HTTP 方法,如下所示:

https://angular.io/api/http/Http#request

您的示例没有帖子正文;我猜您想使用 Http Get。如果不是,您可以更改 HttpMethod适合你的情况

  public GetIndustries() {
return this.http.request(this.baseUrl,{
method : 'Get' // or 'Post'
});
}

或者你可以使用get代替request

public GetIndustries() {
return this.http.get(this.baseUrl);
}

关于Angular 2 Http在主体上获取返回类型3错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50922994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com