- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试发送帖子,但收到以下错误:
2zone-evergreen.js:2952 OPTIONS http://localhost:8080/api/infotel/createOrganisme 403
scheduleTask @ zone-evergreen.js:2952
scheduleTask @ zone-evergreen.js:378
onScheduleTask @ zone-evergreen.js:272
scheduleTask @ zone-evergreen.js:372
scheduleTask @ zone-evergreen.js:211
scheduleMacroTask @ zone-evergreen.js:234
scheduleMacroTaskWithCurrentZone @ zone-evergreen.js:1107
(anonymous) @ zone-evergreen.js:2985
proto.<computed> @ zone-evergreen.js:1428
(anonymous) @ http.js:2065
_trySubscribe @ Observable.js:42
subscribe @ Observable.js:28
(anonymous) @ subscribeTo.js:20
subscribeToResult @ subscribeToResult.js:7
_innerSub @ mergeMap.js:59
_tryNext @ mergeMap.js:53
_next @ mergeMap.js:36
next @ Subscriber.js:49
(anonymous) @ scalar.js:4
_trySubscribe @ Observable.js:42
subscribe @ Observable.js:28
call @ mergeMap.js:21
subscribe @ Observable.js:23
call @ filter.js:13
subscribe @ Observable.js:23
call @ map.js:16
subscribe @ Observable.js:23
create @ create-organisme.component.ts:32
(anonymous) @ CreateOrganismeComponent.html:21
handleEvent @ core.js:34789
callWithDebugContext @ core.js:36407
debugHandleEvent @ core.js:36043
dispatchEvent @ core.js:22533
(anonymous) @ core.js:33721
(anonymous) @ platform-browser.js:1789
invokeTask @ zone-evergreen.js:391
onInvokeTask @ core.js:30885
invokeTask @ zone-evergreen.js:390
runTask @ zone-evergreen.js:168
invokeTask @ zone-evergreen.js:465
invokeTask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629
Show 12 more frames
display:1 Access to XMLHttpRequest at 'http://localhost:8080/api/infotel/createOrganisme' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
core.js:7187 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://localhost:8080/api/infotel/createOrganisme", ok: false, …}
这是java后端 Controller :
package com.infotel.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.infotel.dto.OrganismeDTO;
import com.infotel.service.OrganismeService;
@RestController
@RequestMapping("/api/infotel")
public class Controller {
private final OrganismeService orgService;
public Controller(OrganismeService orgService) {
this.orgService=orgService;
}
@PostMapping("/createOrganisme")
public ResponseEntity<?> create(@RequestBody OrganismeDTO org){
System.out.println(org.toString());
this.orgService.create(org);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With,content-type");
headers.add("Access-Control-Allow-Credentials", "true");
return new ResponseEntity<>(headers,HttpStatus.CREATED);
}
@GetMapping("/getAllOrganismes")
public ResponseEntity<?> getAll(){
List<OrganismeDTO> orgs = this.orgService.getAll();
for(OrganismeDTO o : orgs) {
System.out.println(o.toString());
}
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
return new ResponseEntity<>(orgs,headers, HttpStatus.OK);
}
}
这是 typescript 服务:
import { Injectable } from '@angular/core';
import { Organisme } from './organisme';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable({
providedIn: 'root'
})
export class OrganismeService {
urlGetAll = 'http://localhost:8080/api/infotel/getAllOrganismes';
urlCreate = 'http://localhost:8080/api/infotel/createOrganisme';
constructor(private http: HttpClient) { }
getOrganismes(): Observable<Organisme[]> {
return this.http.get<Organisme[]>(this.urlGetAll);
}
create(org: Organisme): Observable<any>{
httpOptions.headers.append('Access-Control-Allow-Origin', 'http://localhost:8080');
httpOptions.headers.append('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
httpOptions.headers.append('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
httpOptions.headers.append('Access-Control-Allow-Credentials', 'true');
return this.http.post(this.urlCreate, JSON.stringify(org), httpOptions);
}
}
我的标题位于正面和背面,但在 get 未显示错误的情况下出现此错误。
我错过了什么还是我做错了什么?
如果您需要更多信息,请询问我。
最佳答案
在服务器站点使用 @CrossOrigin(origins = "http://localhost:9000 ") ,这将在 API 级别启用 CORS
package com.infotel.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.infotel.dto.OrganismeDTO;
import com.infotel.service.OrganismeService;
@CrossOrigin(origins = "http://localhost:9000")
@RestController
@RequestMapping("/api/infotel")
public class Controller {
private final OrganismeService orgService;
public Controller(OrganismeService orgService) {
this.orgService=orgService;
}
@PostMapping("/createOrganisme")
public ResponseEntity<?> create(@RequestBody OrganismeDTO org){
System.out.println(org.toString());
this.orgService.create(org);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With,content-type");
headers.add("Access-Control-Allow-Credentials", "true");
return new ResponseEntity<>(headers,HttpStatus.CREATED);
}
@GetMapping("/getAllOrganismes")
public ResponseEntity<?> getAll(){
List<OrganismeDTO> orgs = this.orgService.getAll();
for(OrganismeDTO o : orgs) {
System.out.println(o.toString());
}
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
return new ResponseEntity<>(orgs,headers, HttpStatus.OK);
}
}
1 ) 将 origins 替换为您的开发环境主机名和端口,或者您可以引用 https://spring.io/guides/gs/rest-service-cors/
2)对于开发环境,您可以启用 CORS google chrome 插件,并在生产级别之后启用您将其放在同一域中此问题将解决您的问题
关于java - Angular 7 如何将 Access-Control-Allow-Origin header 添加到预检发布请求中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56882101/
我在 swagger.mydomain.com 上运行了 swagger (docker: swaggerapi/swagger-ui),并为在 a.mydomain.com 和 b.mydomain
在我的应用程序中,进行以下两步调用时,我在第二个 AJAX 调用中收到预检错误: $.ajax({ type:'POST', url:'https://podio.com/oauth/
我们在 Istio 中使用 Kubernetes 并配置了一个虚拟服务: http: - match: - uri: prefix: /api rewrite:
我有一个 Firebase Callable Cloud Function我在浏览器的 javascript 应用程序中调用它。 因为请求主机是 ...cloudfunctions.net 而不是我的
这个问题在这里已经有了答案: XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header (11 个答案) 关闭 3
我目前对如何“选择”或选择在请求之前执行 Angular 的(jquery)预检 OPTIONS 调用感到困惑。 我有一个正常的 RESTful api 调用 (api.domain.co) 我已在主
我用 C# 创建了一个 WebService。所有 GET 方法都可以正常工作。 现在我需要提供一些 POST 方法。通过 C# 调用它时,它可以正常工作。然后我尝试用 JavaScript 编写一个
我正在尝试向我的 REST API 发出 POST 请求。这是代码片段(使用 AngularJS): $http({ method: 'POST',
我正在使用 rack-cors 中间件从站点向我的 Rails 应用程序发出跨域请求(尽管我不认为这个问题特定于 rack-cors)。当我用 Firebug 检查控制台输出时,我注意到只有一个请求发
我正在使用 rack-cors 中间件从站点向我的 Rails 应用程序发出跨域请求(尽管我不认为这个问题特定于 rack-cors)。当我用 Firebug 检查控制台输出时,我注意到只有一个请求发
您好,我正在尝试在 Debian 8 Jessie 中设置 DC/OS,我使用 ssh key 建立了 ssh 连接,我能够在没有密码的情况下登录到所有主机和代理(他们正在运行 CentOS 7)。奇
我正在制作一个简单的网络应用程序,它与我无法控制的服务器进行通信。 起初我配置了一个自定义的 $resource GET 请求并且工作正常。现在我需要做一个 POST 请求来从同一台服务器请求一些信息
我正在构建一个带有 Spring Boot 后端的 Angular 2 应用程序。几天来,我一直在尝试解决 CORS 预检的问题。根据这个topic ,它应该像这样与 CORS 过滤器一起工作: @C
我需要将 CORS 过滤器添加到我的 Spring Boot Web 应用程序中。 我添加了如下文档中所述的 CORS 映射 http://docs.spring.io/spring/docs/cur
我正在尝试对我部署的用于检索 JSON 数据的 Azure 函数进行 POST。我收到 CORS 错误。 有权从来源“http://localhost:3000”在“{API URL}”获取数据' 已
我有一个 Angular 应用程序和 Node 后端,所有请求都正常工作,但是当我尝试使用 get 请求并且在其中重定向请求时,会发生“CORS 预检 channel 未成功”此错误。 .当我在 po
我正在尝试获取名为 X-Total-Pages 的自定义 header 当我做GET时 Ajax 调用。调用从客户端发送到 API。API 允许使用以下 header : Access-Control
我正在尝试在 OS X 10.11.3 上运行 Plone 统一安装程序(适用于 4.3.9)不 使用 --without-ssl 选项,并且我收到以下错误: Unable to find libss
我的架构中有三个应用程序。 它们在同一台服务器上,但具有不同的端口号。 A - Token Application (port 4444) - Asp.net WebApi B - API Appli
所以我有一个不寻常的问题,我不确定如何进行调试。我有一个 angular 4 应用程序,它在服务器端使用 cakephp。 在我的应用程序中,我可以访问许多不同的屏幕,除了一个并且它只是 Firefo
我是一名优秀的程序员,十分优秀!