为什么XMLHttpRequest有效,但Angular2 +返回404?

时间:2018-06-12 17:12:23

标签: ajax angular http

我使用angular6。

调用getHello时会返回404错误。

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';

const httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({ providedIn: 'root' })

export class RequisicaoTransporteService {

    constructor(private http: HttpClient) { }

    getHello() : Observable<any> {
        return this.http.post('http://localhost:17844/api/requisicaotransporte/getHello',{},httpOptions)
    };

}

这就是我所说的:

this.requisicaoTransporteService.getHello()
            .subscribe(x => this.hello = x);

enter image description here 当我执行以下操作时,它可以正常工作:

$.ajax({
  type: "POST",
  url: "http://localhost:17844/api/requisicaotransporte/gethello",

success:function(o){console.log(o);}

});

服务器允许CORS。

0 个答案:

没有答案