简体   繁体   English

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

[英]No 'Access-Control-Allow-Origin' header is present on the requested resource

First of all I know that lot of questions have been asked about this, but I really tried to find out what's the problem, but nothing helped me. 首先,我知道已经对此提出了很多问题,但是我确实试图找出问题所在,但是没有任何帮助。 I'm working with C#, ASP.NET, Javascript, AJAX and trying to use an API to get some informations, but I get this error every time. 我正在使用C#,ASP.NET,Javascript,AJAX,并尝试使用API​​来获取一些信息,但是每次都会收到此错误。 When I tried with different API, works perfect. 当我尝试使用其他API时,效果很好。 The API status is active, but response failed every time. API状态为有效,但每次响应均失败。

Error is looking like: 1 错误看起来像: 1

I added permissions in config file: 我在配置文件中添加了权限:

  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>

but this didn't help me. 但这对我没有帮助。 What should I do now? 我现在应该怎么办? I'm trying this for hours, and the same result. 我已经尝试了好几个小时,结果还是一样。

Tried do add jsonp and crossDomain in ajax call, but this didn't help me too. 试图在ajax调用中添加jsonp和crossDomain,但这对我也没有帮助。

    $.ajax({
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },

        crossDomain : true,
        dataType: 'jsonp',
        url: url + id,
        type: 'GET',
        contentType: 'application/json;',
        success: function (valid) {
            if (valid) {
            } else {
            }
        }
    });
}

Thank you all for every answer. 谢谢大家的回答。

Best regards 最好的祝福

If you are using ASP.NET Web Api, try enabling cors in your http config. 如果您使用的是ASP.NET Web Api,请尝试在http配置中启用cors。

        public static void Register(HttpConfiguration config)
    {            
        config.EnableCors();
        config.MapHttpAttributeRoutes() .......

i make ajax calls to such an API successfully like so.. 我像这样成功地对此类API进行了ajax调用。

            $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',

Also Remember to decorate your Api controller with EnableCors Attribute 还请记住用EnableCors属性装饰Api控制器

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class TestController : ApiController
{
    // Controller methods not shown...
}

read more at : http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api 有关更多信息,请访问: http : //www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 角度4-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Angular 4 - No 'Access-Control-Allow-Origin' header is present on the requested resource NodeJ在所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - NodeJs No 'Access-Control-Allow-Origin' header is present on the requested resource Access-Control-Allow-Origin header 存在于请求的资源上 - Access-Control-Allow-Origin header is present on the requested resource 请求的资源 .htaccess 上不存在 Access-Control-Allow-Origin 标头 - No Access-Control-Allow-Origin header is present on the requested resource .htaccess XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头 - XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - Access-Control-Allow-Origin header is not present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - No Access-Control-Allow-Origin header is present on the requested resource 请求的资源 nodejs 上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM