Coder Social home page Coder Social logo

Comments (8)

fmalcher avatar fmalcher commented on May 22, 2024

Hey, thanks for the message. Which combination of paths and methods did you try?
Do you run it locally or do you use our hosted version?

from book-monkey2-api.

stoennies avatar stoennies commented on May 22, 2024

Hey, I tried it both locally and hosted.
While accessing with POSTMAN it works. Using the BookStoreService from the book (translated in Angular 7) it raises the error :-(

BookMonkey runs locally with ng serve

from book-monkey2-api.

fmalcher avatar fmalcher commented on May 22, 2024

Can you please show us the relevant code from the BookStoreService?

from book-monkey2-api.

stoennies avatar stoennies commented on May 22, 2024

Here we go!

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

import { Book, Thumbnail } from './book';
import { BookFactory } from './book-factory';

@Injectable()
export class BookStoreService {
  private api = 'https://book-monkey2-api.angular-buch.com';
  private headers: Headers = new Headers();


  constructor(private http: HttpClient) { }

  private errorHandler(error: Error | any): Observable<any> {
    return throwError(error);
  }

  getAll(): Observable<Array<Book>> {
    return this.http
      .get<Book[]>(`${this.api}/books`)
      .pipe(
        retry(3),
        map(rawBooks => rawBooks
          .map(rawBook => BookFactory.fromObject(rawBook)),
        ),
        catchError(this.errorHandler)
      );
  }

  getSingle(isbn: string): Observable<Book> {
    return this.http
      .get<Book>(`${this.api}/book/${isbn}`)
      .pipe(
        retry(3),
        map(rawBook => BookFactory.fromObject(rawBook)),
        catchError(this.errorHandler)
      );
  }

  create(book: Book): Observable<any> {
    return this.http
      .post(`${this.api}/book`, book, { responseType: 'text' })
      .pipe(
        catchError(this.errorHandler)
      );
  }

  update(book: Book): Observable<any> {
    return this.http
      .put(`${this.api}/book/${book.isbn}`, book, { responseType: 'text' })
      .pipe(
        catchError(this.errorHandler)
      );
  }

  remove(isbn: string): Observable<any> {
    return this.http
      .delete(`${this.api}/book/${isbn}`, { responseType: 'text' })
      .pipe(
        catchError(this.errorHandler)
      );
  }

  getAllSearch(searchTerm: string): Observable<Array<Book>> {
    return this.http
      .get<Book[]>(`${this.api}/book/search/${searchTerm}`)
      .pipe(
        retry(3),
        map(rawBooks => rawBooks
          .map(rawBook => BookFactory.fromObject(rawBook))),
        catchError(this.errorHandler)
      );
  }

}

from book-monkey2-api.

fmalcher avatar fmalcher commented on May 22, 2024

I tried it with the exact same code, both locally and online.
See my example here: https://stackblitz.com/edit/angular-bookstoreservice

Can you reproduce the error in the StackBlitz example?
Can you please post a screenshot of the HTTP request in the Chrome Network tab?

from book-monkey2-api.

stoennies avatar stoennies commented on May 22, 2024

Now I found it!!!!!!
I used the wrong API URL. I used book/search and not books/search :-(
But anyway strange error with CrossSiteScripting....

Thanks a lot for your help!!!

from book-monkey2-api.

fmalcher avatar fmalcher commented on May 22, 2024

Aaah, great!
The Cross Origin error come from the depths of Node and Express... not the clearest error message, though. 😂

from book-monkey2-api.

JohannesHoppe avatar JohannesHoppe commented on May 22, 2024

Yeah, the book-monkey v2 we used for the book has room for improvements.

Request URL: https://book-monkey2-api.angular-buch.com/booksggg

t=147919 [st=206]        HTTP_TRANSACTION_READ_RESPONSE_HEADERS
                         --> HTTP/1.1 404
                             status: 404
                             date: Wed, 14 Nov 2018 21:20:36 GMT
                             content-type: application/json
                             set-cookie: __cfduid=da3b5303c5f15a30bc825d01bf78180ca1542230436; expires=Thu, 14-Nov-19 21:20:36 GMT; path=/; domain=.angular-buch.com; HttpOnly
                             via: 1.1 vegur
                             expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
                             server: cloudflare
                             cf-ray: 479c85e3199f5954-VIE
                             content-encoding: br
t=147919 [st=206]     -HTTP_TRANSACTION_READ_HEADERS

In case of an 404 the backend does not answer with a access-control-allow-origin header at all. So the browser is forced to ignore everything else and has to pretend to be dumb.

I fixed that for v3. Here CORS was implemented correctly:

Request URL: https://api.angular.schule/booksxxx

t=300909 [st=133]        HTTP_TRANSACTION_READ_RESPONSE_HEADERS
                         --> HTTP/1.1 404
                             status: 404
                             date: Wed, 14 Nov 2018 21:23:09 GMT
                             content-type: text/html; charset=utf-8
                             set-cookie: __cfduid=d7d959edf8a4f643d2a26b6fd918690c01542230589; expires=Thu, 14-Nov-19 21:23:09 GMT; path=/; domain=.angular.schule; HttpOnly
                             x-powered-by: Express
                             access-control-allow-origin: *
                             content-security-policy: default-src 'self'
                             x-content-type-options: nosniff
                             via: 1.1 vegur
                             expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
                             server: cloudflare
                             cf-ray: 479c899fbc9059a8-VIE
                             content-encoding: br
t=300909 [st=133]     -HTTP_TRANSACTION_READ_HEADERS

from book-monkey2-api.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.