简体   繁体   English

本地主机api请求的HTTP失败响应:0未知错误-Angular

[英]Http failure response for localhost api request: 0 Unknown Error - Angular

I'm trying to create a communication to my Java Jetty Backend from my Angular application. 我正在尝试从Angular应用程序创建到Java Jetty后端的通信。 When I try to execute my request I receive the following error: 当我尝试执行请求时,出现以下错误:

错误要求

My code on client side: (Angular 7.2.1). 我在客户端的代码:(Angular 7.2.1)。 I'm also using a HttpInterceptor for authentication that should work. 我还在使用HttpInterceptor进行身份验证,该方法应该可以正常工作。 I'm also running the code in development mode with ng serve . 我也在ng serve以开发模式运行代码。

 @Injectable({ providedIn: 'root' }) export class NgHydrantService { constructor(private http: HttpClient) { } public register(entity: IEntityDescription): Observable<StandardResponsePacket> { let packet = new RegisterEntityRequestPacket(entity); return this.http.post(this._apiUrl, packet.toJson()) .pipe( map(value => { console.log('register result:', value); //<-- never executed return <StandardResponsePacket>HydrantPackage.fromJson(value) }) ); } } //The interceptor intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { // add authorization header with basic auth credentials if available if (this.user != null) { const clonedRequest = request.clone({ headers: request.headers.set('Authorization', `Basic ${this.user.getAuth()}`) .set('Accept','application/json') }); //This debug line works and looks good! console.log('NgHydrantAuthInterceptor#intercept', clonedRequest); return next.handle(clonedRequest); } return next.handle(request); } 

My Code on server side: (Jetty-9.4.14.v20181114) that runs on localhost. 我在服务器端的代码:(Jetty-9.4.14.v20181114)在本地主机上运行。

public final class PacketHandler extends AbstractHandler
{
  @Override
  public void handle( String target,
                    Request baseRequest,
                    HttpServletRequest request,
                    HttpServletResponse response ) throws IOException
  {
    try
    {
        // Declare response encoding and types
        response.setContentType( "application/json; charset=utf-8" );
        // Enable CORS
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
        response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
        response.addHeader("Access-Control-Max-Age", "1728000");
        //... more stuff
    }
    finally
    {
        // Inform jetty that this request was handled
        baseRequest.setHandled( true );
    }
  }
}

Things I checked: 我检查过的事情:

  • During research some people reference problems with CORS (that's why I added the header entries in the server side code) 在研究过程中,有些人提到了CORS的问题(这就是为什么我在服务器端代码中添加头条目的原因)
  • The same request in Postman works without any problems 邮递员中的相同请求可以正常工作
  • There are no logs on the server side 服务器端没有日志

My question is about a possible solution to get responses from my server during development. 我的问题是有关在开发期间从服务器获取响应的可能解决方案。

i was able to solve a likely problem by setting my web server configuration to allow the cors policy. 通过设置Web服务器配置以允许使用cors策略,我能够解决可能的问题。 In my case, i use Nginx web server follow the configuration in this link https://enable-cors.org/server_nginx.html 就我而言,我使用Nginx Web服务器遵循此链接中的配置https://enable-cors.org/server_nginx.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 http:// localhost:8080 / mix / api / get / holidaytype / all响应CoSend&#39;GET&#39;请求发送到URL:de:405 - http://localhost:8080/mix/api/get/holidaytype/all Response CoSending 'GET' request to URL :de : 405 尝试将请求 /api/v1/mega/building 从 localhost:4200 代理到 http://localhost:8080 时发生错误(ECONNREFUSED) - Error occurred while trying to proxy request /api/v1/mega/building from localhost:4200 to http://localhost:8080 (ECONNREFUSED) 在 IIS 服务器上运行的 Angular FE:对 http://localhost:8080/api 的 POST 请求生成 net::ERR_CONNECTION_REFUSED - Angular FE running on IIS server: POST request to http://localhost:8080/api generates net::ERR_CONNECTION_REFUSED 为什么我的带有 android volley 的 http post 请求会引发错误(localhost)? - Why my http post request with android volley throws error (localhost)? Java在localhost上的HTTP响应无效 - Java Invalid HTTP response on localhost http url 的 Http 失败响应:401 未经授权 - Http failure response for http url: 401 Unauthorized HTTP 路由优化的 startOptimization 请求出现错误 411 api - HTTP Error 411 for startOptimization request of routeoptimization api HTTP响应代码400将GET请求发送到HTTPS查询API - HTTP response code 400 sending GET Request to HTTPS Query API 带有JSON响应的api.stackexchange上的Java Http请求 - Java Http request on api.stackexchange with json response 无法处理请求[GET http:// localhost:8080]:响应状态404 Spring,RESTfull,thymleaf - Failed to handle request [GET http://localhost:8080 ]: Response status 404 Spring, RESTfull ,thymleaf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM