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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机