【问题标题】:Service can't reach post function服务无法达到发布功能
【发布时间】:2017-10-04 00:04:22
【问题描述】:

嘿(第一次在这里发帖,所以我可能会违反一些规则,如果我这样做,请告诉我),

我正在尝试创建一个 Rest API,但遇到了一些问题。 事实上,post 函数并没有在 C# API 上触发,我不知道为什么与 getAll() 函数一起使用的 GET 函数运行良好。

角度服务:

public GetAll = (): Observable<Expertise[]> => {
        return this.http.get(this.actionUrl, '').map((response: Response) => <Expertise[]>response.json());
}

public Add = (thingToAdd: Expertise): Observable<Expertise> => {
    thingToAdd.Id = 1;
    let toAdd = JSON.stringify(thingToAdd);
    console.log(toAdd);
    console.log(this.actionUrl);
    return this.http.post(this.actionUrl, toAdd, { headers: this.headers 
}).map((response: Response) => response.json());
}

C# API:

// GET api/values
    [HttpGet]
    public async Task<IEnumerable<Expertise>> Get()
    {
        try
        {
            System.Console.WriteLine("Test get all");
            //var result = await cvService.Get("toto@azeo.com");
            return new List<Expertise>();
        }
        catch (System.Exception ex)
        {
            logger.LogError(ex.Message, ex);
            throw;
        }
    }
// POST api/values
    [HttpPost]
    public Expertise Post([FromBody]Expertise expertise)
    {
        try
        {
            System.Console.WriteLine("Test post");
            context.Expertises.Add(expertise);
            context.SaveChanges();

        logger.LogInformation("New expertise added !");
        return expertise;
        }
        catch (System.Exception ex)
        {
            logger.LogError(ex.Message, ex);
            throw;
        }
    }

专业知识(EF 模型):

public class Expertise
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<ExpCV> CVs { get; set; }
}

如果有人有“链接”服务的想法并且我的 API 告诉我,我已经坚持了很长时间。

提前谢谢你

【问题讨论】:

  • http请求执行了吗?回报是多少?
  • post 好像没有被执行,get 被执行了
  • 你如何称呼你的 'getAll' 和 'add' ?您必须订阅它们中的每一个才能进行调用。
  • ngOnInit() { this.expertise = new Expertise(); this.listeExpertise = this.service.GetAll(); } 公共 addExpertise() { this.service.Add(this.expertise); } @AhmedMusallam
  • 此代码来自 typescript 中的组件

标签: c# angular typescript angular2-services azure-api-apps


【解决方案1】:

由于您的服务返回 observable,实际的 GETPOST 请求在您订阅该 observable 之前不会发生。

看看我设置的这个 plunker:

plunker:https://plnkr.co/edit/r6aBaB3BjJzdIS4myAd8?p=preview

代码:

@Component({
    selector: 'app',
    template: `
        <button (click)="searchNoSub()">searchNoSub</button>
        <button (click)="search()">search</button>
    `
})

export class App {

    constructor(private searchService:SearchService) {
    }

    search(){
        this.searchService.search().subscribe(console.log,console.log)
    }
    searchNoSub(){ this.searchService.search(); }
}

搜索服务:

@Injectable()
export class SearchService {

  constructor(private http: Http) {}

  search(term: string) {
    // get artists named john from spotify API
    return this.http.get('https://api.spotify.com/v1/search?q=john&type=artist')
      .map((response) => response.json());
  }
}

现在在 chrome 中打开您的 devtools 和 network 选项卡以查看新请求:

单击searchNoSub 时,您会注意到网络选项卡中没有注册请求。 单击search 按钮时,您会看到一个请求,因为我们已订阅。

简而言之,你需要订阅可观察到的请求。

【讨论】:

  • 非常感谢您的完整回答:D
猜你喜欢
  • 2016-05-31
  • 2020-08-08
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多