我正在使用Spring MVC和角7创建一个CRUD应用程序。我在我的Spring应用程序中允许CORS,但是当我从角调用PUT请求时,我得到一个“从源'http://localhost:4200'在'http://localhost:8080/BookAPI/api/updateBook/70'处访问XMLHttpRequest已被CORS策略阻止:对预飞行请求的响应未通过权限改造检查:请求的资源上不存在'Access-Control-Allow-Origin'标头”错误
我已经在spring mvc应用程序中启用了CORS
程序包com。书Spring控制器;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.book.spring.models.Book;
import com.book.spring.service.BookService;
@CrossOrigin("*")
@RequestMapping("/api")
@RestController
public class BookController {
@Autowired
private BookService bookService;
// Save a book
@PostMapping("/create")
public ResponseEntity<String> createBook(@RequestBody Book book) {
Long bookId = bookService.create(book);
return ResponseEntity.ok().body("Book created with ID =" + bookId);
}
// Get All books
@GetMapping("/getBooks")
public ResponseEntity<List<Book>> listbooks() {
List<Book> list = bookService.getAllBooks();
return ResponseEntity.ok().body(list);
}
// Get a book by its ID
@GetMapping("/getBookByID/{id}")
public ResponseEntity<Book> getBookById(@PathVariable("id") Long id) {
Book book = bookService.getBookById(id);
return ResponseEntity.ok().body(book);
}
// Update a book
@PutMapping("/updateBook/{id}")
public ResponseEntity<?> updateBook(@PathVariable("id") Long id, @RequestBody Book book) {
bookService.updateBook(id, book);
return ResponseEntity.ok().body("Book updated");
}
// Delete a book
@DeleteMapping("/deleteBook/{id}")
public ResponseEntity<?> deleteBook(@PathVariable("id") Long id) {
bookService.deleteBook(id);
return ResponseEntity.ok().body("Book has be deleted");
}
}
bookservice.ts:
import { Injectable } from '@angular/core';
import * as UrlConstants from './urls';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { Book } from './book/book';
import { catchError } from 'rxjs/operators';
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*'
});
@Injectable({
providedIn: 'root'
})
export class BookService {
private _getBooksUrl: string = UrlConstants.BOOKS_URL;
private _postBooksUrl: string = UrlConstants.POST_BOOK_URL;
private _deleteBooksUrl: string = UrlConstants.DELETE_BOOK_URL;
private _getBookUrl: string = UrlConstants.GET_BOOK_URL;
private _putBookUrl: string = UrlConstants.PUT_BOOK_URL;
constructor(private _http: HttpClient) { }
getAllBooks(): Observable<Book[]> {
console.log(this._getBooksUrl);
return this._http.get<Book[]>(this._getBooksUrl)
.pipe(catchError(this.errorHandler));
}
addBook(book: Book) {
console.log("adding book");
if (book.id) {
return this._http.post(this._putBookUrl + book.id, {"title":book.title,"author":book.author}, { responseType: 'text',headers:headers})
.pipe(catchError(this.errorHandlerPost));
} else {
return this._http.post(this._postBooksUrl, book, { responseType: 'text' })
.pipe(catchError(this.errorHandlerPost));
}
}
deleteBook(id: string) {
return this._http.delete(this._deleteBooksUrl + id, { responseType: 'text' })
.pipe(catchError(this.errorHandlerPost));
}
getBookById(bookId: string): Observable<Book> {
return this._http.get<Book>(this._getBookUrl + bookId)
.pipe(catchError(this.errorHandlerPost));
}
errorHandler(errorHandler: HttpErrorResponse): Observable<Book[]> {
return throwError(errorHandler.message || "server error");
}
errorHandlerPost(errorHandler: HttpErrorResponse) {
return throwError(errorHandler.message || "server error");
}
}
consts:
export const BOOKS_URL = 'http://localhost:8080/BookAPI/api/getBooks';
export const POST_BOOK_URL = 'http://localhost:8080/BookAPI/api/create';
export const DELETE_BOOK_URL ='http://localhost:8080/BookAPI/api/deleteBook/';
export const GET_BOOK_URL ='http://localhost:8080/BookAPI/api/getBookByID/';
export const PUT_BOOK_URL = 'http://localhost:8080/BookAPI/api/updateBook/';
它应该允许所有请求
你应该用这个_http。put for put请求,您不应该使用post客户端进行put请求。
更改自
返回此。_http.post(this._putBookUrlbook.id,{"title":book.title,"作者":book.author},{响应类型:'text',标头:标头})。管道(catchError(this.errorHandlerPost));
返回此_http。put(this.\u putBookUrl book.id,{“title”:book.title,“author”:book.author},{响应类型:“text”,标题:headers})。管道(catchError(this.errorHandlerPost));
尝试在API响应中设置“访问控制允许方法”标头以允许PUT请求。
经过两天的反复搜索。我终于找到了答案!!
WebDAVModule本身存在问题,默认情况下,它似乎会阻止PUT和DELETE方法!
<system.webServer>
<modules runAllManagedModulesForAllRequests="false">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
我真的希望没有人会因此而感到痛苦!=]
方特:https://mozartec.com/asp-net-core-error-405-methods-not-allowed-for-put-and-delete-requests-when-hosted-on-iis/
我有一个Android、Ios和web应用程序,它使用php作为后端。所有Api在android和ios中都运行良好,但在web中抛出CORS错误。得到这样的错误 访问位于“”的XMLHttpRequesthttps://example.com/api“起源”http://localhost:49168'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origi
我有一个flask socketio服务器,运行在带有nginx的ubuntu上。我有一个客户端调用服务器。当我尝试呼叫服务器时,收到以下错误: 这是我对烧瓶的初始化: ... 这是我当前在nginx中的配置: 来自客户端对服务器的请求如下所示: 我尝试过: -添加允许CORS到位置/仅,以及两者-在烧瓶应用程序中删除/添加allow CORS origin。 有人能帮忙吗?
我在Axios Laravel crossite中遇到问题,它返回以下erorr: CORS 策略阻止了从源“http://localhost:8000”在“http://localhost/moddle/moodle/login/index.php”处访问 XMLHttpRequest:对预检请求的响应未通过访问控制检查:请求的资源上不存在“访问控制-允许-源”标头。 我在stackoverfl
它是一个服务类,如下所示: 我试图通过将用户名和密码添加为let来发送用户名和密码,但它说我被CORS策略阻止了。 在这里,我在我的春靴里补充道: 在controller类中: 来自网络选项卡的消息
我试图部署一个API用于前端。当单独测试API时,它是工作的,但当集成到Vue应用程序中时,它返回CORS错误,错误返回是: CORS策略阻止从来源“http://localhost:8080”访问位于“https://apidomain/development/pin”的XMLHttpRequest:请求的资源上没有“Access-Control-Allow-Origin”标头。 我使用serv
调用JSON时发生此错误。我不知道为什么会发生这个错误。 错误: