我试图通过连接到Flask内置的RESTful API来使用JavaScript进行授权.但是,当我发出请求时,我收到以下错误:
XMLHttpRequest无法加载http:// myApiUrl/login.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.
我知道API或远程资源必须设置标题,但为什么在我通过Chrome扩展程序Postman发出请求时它可以正常工作?
这是请求代码:
$.ajax({
type: "POST",
dataType: 'text',
url: api,
username: 'user',
password: 'pass',
crossDomain : true,
xhrFields: {
withCredentials: true
}
})
.done(function( data ) {
console.log("done");
})
.fail( function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
alert(textStatus);
});
Run Code Online (Sandbox Code Playgroud) 我们添加Spring Security到现有项目中.
从这一刻起,我们No 'Access-Control-Allow-Origin' header is present on the requested resource从服务器收到401 错误.
那是因为没有Access-Control-Allow-Origin标题附加到响应.为了解决这个问题,我们Filter在注销过滤器之前添加了我们自己的过滤器,但过滤器不适用于我们的请求.
我们的错误:
XMLHttpRequest无法加载
http://localhost:8080/getKunden.请求的资源上不存在"Access-Control-Allow-Origin"标头.http://localhost:3000因此不允许原点访问.响应具有HTTP状态代码401.
我们的安全配置:
@EnableWebSecurity
@Configuration
@ComponentScan("com.company.praktikant")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private MyFilter filter;
@Override
public void configure(HttpSecurity http) throws Exception {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
source.registerCorsConfiguration("/**", config);
http.addFilterBefore(new MyFilter(), LogoutFilter.class).authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/*").permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws …Run Code Online (Sandbox Code Playgroud) 我使用React和Express遇到了同构JavaScript应用程序的问题.
我正在尝试使用axios.get在我的组件安装时发出HTTP请求
componentDidMount() {
const url = 'http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders';
axios.get(url).then( res => {
//use res to update current state
})
}
Run Code Online (Sandbox Code Playgroud)
我从API获得状态200 res,但我没有得到任何响应数据并在我的控制台中收到错误
XMLHttpRequest cannot load http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
但是,如果我在server.js中发出请求
const url = 'http://ufc-data-api.ufc.com/api/v3/iphone/fighters/title_holders';
axios.get(url).then(res => {
//console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
它工作正常,我在服务器启动时获得响应数据.这是实际API的问题还是我做错了什么?如果这是一个CORS问题,我猜测server.js中的请求也不起作用?谢谢!
我正在使用 i mgur api通过节点 js 应用程序上传图像。
我正在将图像转换为 base64 字符串并通过 Postman 发送它们效果很好。
我node-fetch用来进行api调用。
const fetch = require('node-fetch')
...
async uploadImage(base64image) {
try {
const url = 'https://api.imgur.com/3/image'
const res = await fetch(url,
{
method: 'POST',
body: { image: base64image },
headers: {
'content-type': 'application/json',
'Authorization': 'Client-ID [my-client-id]',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Access-Control-Allow-Headers',
'Access-Control-Allow-Methods': 'POST',
}
}
)
console.log(res)
} catch(err) {
console.log(err)
}
}
Run Code Online (Sandbox Code Playgroud)
错误:从源“ http://localhost:3000 ”获取“ https://api.imgur.com/3/image ”的访问已被 CORS 策略阻止:请求标头字段Access-Control-Allow-Headers为预检响应中的Access-Control-Allow-Headers 不允许。 …
我在控制台中得到这个:
Failed to load https://api.imgur.com/3/image: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'https://example.org' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
var formData = new FormData();
formData.append('image', $('#imgur-api-upload')[0].files[0]);
formData.append('type', 'file');
formData.append('name', $('#imgur-api-upload')[0].files[0].name.replace('.jpg', ''));
// request
$.ajax({
async: true,
crossDomain: true,
url: 'https://api.imgur.com/3/image',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + imgur_access_token
},
processData: false,
contentType: false,
mimeType: 'multipart/form-data',
data: formData
})
.done(function(dataResponse) {
console.log(dataResponse);
if (dataResponse.hasOwnProperty('status') && dataResponse.hasOwnProperty('success')) {
if (dataResponse['success'] == true && dataResponse['status'] == 200) {
$('#episode_image').val(dataResponse['data']['link']);
} …Run Code Online (Sandbox Code Playgroud) 我正在使用一个项目模块,我想在该模块中根据货币代码和金额从 Google 财务转换器获取换算后的价格。\n这是我的代码:
\n\n$(\'.actual-price\').tooltip({\n content: function(callback) {\n var url = \'https://www.google.com/finance/converter?a=25&from=USD&to=AMD\';\n $.get(url, {}, function(data) { \n callback(data);\n });\n },\n open: function(event, ui) {\n var $id = $(ui.tooltip).attr(\'id\');\n $(\'div.ui-tooltip\').not(\'#\' + $id).remove();\n },\n close: function(event, ui) {\n $(\'.ui - tooltip\').hide();\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n\n它给了我一个错误:
\n\n\n\n\nXMLHttpRequest 无法加载\n
\nhttps://www.google.com/finance/converter?a=25&from=USD&to=AED。所请求的资源上不存在“Access-Control-Allow-Origin”标头。http://mytestsite.com因此不允许源访问。
我尝试过以下方法来解决它,但似乎没有什么对我有用!
\n\n首先:\n添加Access-Control-Allow-Origin到网络配置中。
<httpProtocol>\n <customHeaders>\n <add name="Access-Control-Allow-Origin" value="*" />\n </customHeaders>\n</httpProtocol>\nRun Code Online (Sandbox Code Playgroud)\n\n第二:使用数据类型 jsonp:
\n\ndataType: "jsonp",\nRun Code Online (Sandbox Code Playgroud)\n\n并已经提到了以下帖子:
\n\n\xe2\x80\x9c请求的资源上不存在“Access-Control-Allow-Origin\”标头\xe2\x80\x9d
\n\n\n\n我正在尝试创建一个自定义的广播播放器,该播放器会自动更新当前流式音频的标题和插图。
Shoutcast确实有一个API,但是它只能使用其Dev ID,但是Shoutcast最近显然没有提供任何Dev ID。所以我需要另一个解决方法。
有一些PHP解决方案,但是由于我不了解该语言,所以无法找到他们的解决方案。请提供一些有关如何获取当前曲目标题,插图甚至艺术家姓名的示例或提示。
提前致谢
javascript ×6
cors ×3
jquery ×3
api ×2
imgur ×2
node.js ×2
ajax ×1
asp.net-mvc ×1
audio ×1
axios ×1
html5 ×1
java ×1
node-fetch ×1
reactjs ×1
shoutcast ×1
spring ×1
spring-boot ×1