![](/img/trans.png)
[英]Problem with Spring Boot Security and CORS when it comes to POST requests
[英]Getting CORS error in Chrome when using AJAX requests to a Spring Boot application
我試圖在本地運行一個Spring Boot應用程序,並且我有一些問題將AJAX請求發回給應用程序。 Chrome給了我以下錯誤(實際上是來自jquery):
Failed to load localhost:8080/api/input: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
我正在嘗試發出一個POST請求,該請求將由以下方法處理。 這實際上適用於Postman。
@RequestMapping(value = "/api/input", method = RequestMethod.POST)
public @ResponseBody UserInputModel getUserInput(@RequestBody UserInputModel input)
{
input.setMessage("bye");
return input;
}
這是發出請求的腳本:
$(document).ready(function () {
$('#submitInput').click(function () {
// Only send the message to the server if the user typed something
var userMessage = {
message: $('#userInput').val()
};
console.log(userMessage);
if (userMessage.message) {
console.log("Sending request");
$.ajax({
url: "localhost:8080/api/input",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: JSON.stringify(userMessage),
type: 'POST',
success: function() {
console.log("yay");
},
error: function(err) {
console.log(err);
}
});
} else {
console.log("Empty input, no request sent");
}
});
console.log("Loaded testScript.js");
});
所以這就是有效的。 我有另一個控制器返回一個html頁面(也加載我的腳本)。 這在Chrome中有效,我使用文本框和按鈕獲取我的頁面,並且腳本確實已加載。 問題是腳本返回應用程序的請求不起作用。
這是pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0
<groupId>my.group</groupId>
<artifactId>appname</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>appname</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
您可以使用JSONP: https : //dev.socrata.com/docs/cors-and-jsonp.html
或者您可以在Spring啟動中指定顯式配置:
@Configuration
public class RestConfig {
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.addAllowedMethod("PUT");
config.addAllowedMethod("DELETE");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.