相关疑难解决方法(0)

为什么我的JavaScript在所请求的资源上出现"No'Access-Control-Allow-Origin'标头"错误,当Postman没有?

我试图通过连接到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)

javascript jquery same-origin-policy cors flask-restless

2320
推荐指数
29
解决办法
399万
查看次数

在Chrome中停用相同的来源政策

有没有办法在Google的Chrome浏览器上禁用同源策略

这是严格的开发,而不是生产用途.

javascript ajax google-chrome

1443
推荐指数
29
解决办法
177万
查看次数

Spring安全CORS过滤器

我们添加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)

java spring spring-security cors spring-boot

22
推荐指数
10
解决办法
8万
查看次数

从React(Isomorphic应用程序)进行API调用时出现"Access-Control-Allow-Origin"问题

我使用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中的请求也不起作用?谢谢!

javascript node.js reactjs isomorphic-javascript axios

19
推荐指数
6
解决办法
6万
查看次数

POST 到外部 API 会抛出 CORS,但它可以从 Postman 工作

我正在使用 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 不允许。 …

javascript node.js imgur cors node-fetch

13
推荐指数
2
解决办法
3万
查看次数

Imgur API无法加载

我在控制台中得到这个:

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)

api jquery imgur

9
推荐指数
1
解决办法
701
查看次数

“Access-Control-Allow-Origin”Google 财务转换器给出错误 javascript/jquery

我正在使用一个项目模块,我想在该模块中根据货币代码和金额从 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});\n
Run Code Online (Sandbox Code Playgroud)\n\n

它给了我一个错误:

\n\n
\n

XMLHttpRequest 无法加载\n https://www.google.com/finance/converter?a=25&from=USD&to=AED。所请求的资源上不存在“Access-Control-Allow-Origin”标头。http://mytestsite.com因此不允许源访问。

\n
\n\n

我尝试过以下方法来解决它,但似乎没有什么对我有用!

\n\n

首先:\n添加Access-Control-Allow-Origin到网络配置中。

\n\n
<httpProtocol>\n    <customHeaders>\n        <add name="Access-Control-Allow-Origin" value="*" />\n    </customHeaders>\n</httpProtocol>\n
Run Code Online (Sandbox Code Playgroud)\n\n

第二:使用数据类型 jsonp:

\n\n
dataType: "jsonp",\n
Run Code Online (Sandbox Code Playgroud)\n\n

并已经提到了以下帖子:

\n\n

\xe2\x80\x9c请求的资源上不存在“Access-Control-Allow-Origin\”标头\xe2\x80\x9d

\n\n

请求的资源错误上不存在“Access-Control-Allow-Origin”标头

\n\n

请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin …

javascript asp.net-mvc jquery google-finance

6
推荐指数
1
解决办法
901
查看次数

如何在JS中获取Shoutcast当前曲目标题和插图

我正在尝试创建一个自定义的广播播放器,该播放器会自动更新当前流式音频的标题和插图。

Shoutcast确实有一个API,但是它只能使用其Dev ID,但是Shoutcast最近显然没有提供任何Dev ID。所以我需要另一个解决方法。

有一些PHP解决方案,但是由于我不了解该语言,所以无法找到他们的解决方案。请提供一些有关如何获取当前曲目标题,插图甚至艺术家姓名的示例或提示。

提前致谢

javascript api audio html5 shoutcast

1
推荐指数
1
解决办法
4046
查看次数