m0_71681226 2022-09-26 15:04 采纳率: 100%
浏览 59
已结题

在使用angular从API获取数据时失败

在使用angular从API获取数据时失败

data.service文件(Typescript)
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { BookData } from './BookData';

@Injectable()


export class DataService{

private url:string = "http://localhost:3060/api/concert/";
constructor (private http:HttpClient){
}
public getAllBooks():Observable<BookData>{
return this.http.get<BookData>(this.url);
}
public getBooksById(category_ID:number):Observable<BookData>{
return this.http.get<BookData>(this.url+category_ID);
}
public deleteBook(book_ID:number):Observable<BookData>{
return this.http.delete<BookData>(this.url+book_ID);
}
public updateBook(book_ID:number,price:string):Observable<BookData>{
return this.http.put<BookData>(this.url,{"book_ID":book_ID,"price":price});
}
public insertBook(book_ID:number,title:string,author:string,price:string,description:string,category_ID:number):Observable<BookData>{
return this.http.post<BookData>(this.url,{"book_ID":book_ID,"title":title,"author":author,"price":price,"description":description,"category_ID":category_ID});
}
}
app.compent文件(Typescript)
import { Component } from '@angular/core';
import { DataService } from './data.service';
import { FormsModule } from '@angular/forms';
import { BookData } from './BookData';
@Component({
  selector: 'app-root',
  template: 'Hello! This is my Angular JS

List of all books <input type="button" value="Get all books" (click)="getAllBooks()">{{allBooks}}{{errorMessage}}'
, styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'bookstore'; allBooks:BookData[]=new Array(); errorMessage:string=""; constructor(private dataService:DataService){ } getAllBooks(){ this.dataService.getAllBooks().subscribe( (d:any)=>{ this.allBooks=d; }, (err:any)=>{ this.errorMessage="No records found"; } ); } }
BookData文件(Typescript)
export class BookData{
book_ID:number;
title:string;
author:string;
price:string;
description:string;
category_ID:number;

constructor(b:number, t:string, a:string, p:string, d:string, c:number)
{
     this.book_ID=b;
     this.title=t;
     this.author=a;
     this.price=p;
     this.description=d;
     this.category_ID=c;
}
}
API中的GET方法,改API和一个名为bookstore_db的数据库连接在一起了
router.get("/", (req, res)=>{
    connection.query("select * from bookstore_db.book", (err, records,fields)=> {
         if (err){
             console.error("Error while retrieve the data");
         }else{
             res.send(records);
         }
    })
})

postman测试API里是有数据的

img


但在angular的页面里点击按钮取不到数据

img

请问怎么在angular中GET到数据

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-09-26 15:15
    关注

    浏览器有跨域问题,接口网址和当前项目端口一样吗?不一样跨域会出错,具体看浏览器开发工具控制台是否报错,网络选项卡的xhr请求状态是否200

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • MAXLZ 2022-09-26 15:15
    关注

    F12,检查页面是否发送方了请求

    评论
  • 有问必答小助手 2022-09-26 15:25
    关注
    您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
    PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月26日
  • 已采纳回答 9月26日
  • 创建了问题 9月26日

悬赏问题

  • ¥15 如何通过求后验分布求得样本中属于两种物种其中一种的概率?
  • ¥15 q从常量变成sin函数,怎么改写python代码?
  • ¥15 图论编程问题,有可以指导的吗
  • ¥15 DEA的CCR模型画图
  • ¥15 请假各位一个关于安卓车机的问题
  • ¥15 光谱仪怎么看这样的透射谱
  • ¥15 pyqt5 如何实现输入框输入关键词,下拉框显示模糊查询返回的结果?
  • ¥20 fluent模拟,可以燃烧和相变同时模拟吗?
  • ¥50 海康摄像头,C#如何识别车牌号码和抓取JPG
  • ¥15 yolov5 pt转engine的问题