{Angular}错误试图连接到服务器(解析过程中的HTTP故障)

发布于 2025-02-01 02:03:09 字数 2080 浏览 3 评论 0 原文

我正在尝试制作一个动态的网页。我在本地JSON中有数据,我想使用JSON服务器连接到模拟服务器。

如果我使用本地地址(例如“ SRC/Assets/data/data.json”),则没有错误,我可以访问而没有并发症。当我想使用地址“ http:// localhost:5000”执行相同操作时,问题就来了。

错误对象{标题:{…},状态:200,statustext:“ ok”,url:“ http:// localhost:5000/”,ok:ok ok:false,name:“ httperrorresponse”,message:“在解析http:// localhost:5000/“,错误:{…}}

我搜索信息但没有找到(或不明白)如何修复它。我不知道它是否会有所帮助(并且在Internet上找到了它),但是如果我在插值代码中添加“ | json”,则结果是“ null”。好吧,我留下我的代码。任何帮助都将受到赞赏。

一个示例JSON:

{
  "block":{
    "block1":"block1",
    "block2":"block2",
    "block3":"block3"
  }
}

我的服务:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MyserviceService {
  private apiUrl = 'http://localhost:5000/'
  constructor(private http:HttpClient) { }

  getData():Observable<any>{
    return this.http.get(this.apiUrl)
  }
}

我的组件模板“连接”:

<h1 style="text-align: center;">{{myTest?.block.block1}}</h1>
<h1 style="text-align: center;">{{myTest?.block.block2}}</h1>
<h1 style="text-align: center;">{{myTest?.block.block3}}</h1>

我的组件TS:

import { MyserviceService } from './../../services/myservice.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-connection',
  templateUrl: './connection.component.html',
  styleUrls: ['./connection.component.css']
})
export class ConnectionComponent implements OnInit {
  myTest:any;
  constructor(private dataTest:MyserviceService) { }

  ngOnInit(): void {
    this.dataTest.getData().subscribe(data => {
      this.myTest = data;
    });
  }
}

我还在Stackblitz中重新创建了它。

I'm trying to make a dynamic web page. I have the data in a local json and I want to connect to a mock server using json-server.

If I use a local address (like "src/assets/data/data.json") there are no errors and I can access without complications. The problem comes when I want to do the same using the address "http://localhost:5000".

ERROR Object { headers: {…}, status: 200, statusText: "OK", url: "http://localhost:5000/", ok: false, name: "HttpErrorResponse", message: "Http failure during parsing for http://localhost:5000/", error: {…} }

I searched for information but didn't find (or didn't understand) how to fix it. I don't know if it will help (and I found it on the Internet) but if I add "|json" to the interpolation code I get "null" as a result. Well, I leave my code. Any help is appreciated.

An example json:

{
  "block":{
    "block1":"block1",
    "block2":"block2",
    "block3":"block3"
  }
}

My service:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MyserviceService {
  private apiUrl = 'http://localhost:5000/'
  constructor(private http:HttpClient) { }

  getData():Observable<any>{
    return this.http.get(this.apiUrl)
  }
}

My component template "connection":

<h1 style="text-align: center;">{{myTest?.block.block1}}</h1>
<h1 style="text-align: center;">{{myTest?.block.block2}}</h1>
<h1 style="text-align: center;">{{myTest?.block.block3}}</h1>

My component ts:

import { MyserviceService } from './../../services/myservice.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-connection',
  templateUrl: './connection.component.html',
  styleUrls: ['./connection.component.css']
})
export class ConnectionComponent implements OnInit {
  myTest:any;
  constructor(private dataTest:MyserviceService) { }

  ngOnInit(): void {
    this.dataTest.getData().subscribe(data => {
      this.myTest = data;
    });
  }
}

I have also recreated it in Stackblitz.

https://stackblitz.com/edit/angular-tc-fd?file=src%2Fapp%2Fservices%2Fmyservice.service.ts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2025-02-08 02:03:09

这有点令人尴尬,我不知道它是否“正确”,但是我会回答自己,因为我能够通过一个简单的问题来解决它。

JSON-Server有一个欢迎页面,在我的情况下,该页面将在地址“ Localhost:5000”。问题在于我的程序想从“欢迎”页面获取信息,而不是JSON。但是,如果我添加“ Localhost:5000/block”中的资源,它确实显示了它,我的代码看起来像这样。

export class MyserviceService {
  private apiUrl = 'http://localhost:5000/block'
  constructor(private http:HttpClient) { }

  getData():Observable<any>{
    return this.http.get(this.apiUrl)
  }
}

加上模板中的小更改。

<h1 style="text-align: center;">{{myTest?.block1}}</h1>
<h1 style="text-align: center;">{{myTest?.block2}}</h1>
<h1 style="text-align: center;">{{myTest?.block3}}</h1>

就是这样。

尽管这不是我所期望的,但至少我解决了并学到了它。

This is a bit embarrassing, and I don't know if it's "correct", but I'll answer myself since I was able to solve it thanks to a simple question I was asked.

json-server has a welcome page, which in my case would be at the address "localhost:5000". The problem was that my program wanted to get information from the welcome page which is not json. But if I add the resource, which is in "localhost:5000/block", it does show it and my code would look like this.

export class MyserviceService {
  private apiUrl = 'http://localhost:5000/block'
  constructor(private http:HttpClient) { }

  getData():Observable<any>{
    return this.http.get(this.apiUrl)
  }
}

Plus a small change in the template.

<h1 style="text-align: center;">{{myTest?.block1}}</h1>
<h1 style="text-align: center;">{{myTest?.block2}}</h1>
<h1 style="text-align: center;">{{myTest?.block3}}</h1>

And that's it.

Although it was not what I expected, at least I solved it and learned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文