简体   繁体   English

出现错误无法加载资源:请求的资源上不存在“ Access-Control-Allow-Origin”标头

[英]getting error Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource

I am getting the following error: 我收到以下错误:
Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource. 无法加载资源:请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'domain' is therefore not allowed access. 因此,不允许访问原始“域”。 domain/api/login XMLHttpRequest cannot load api/login. domain / api / login XMLHttpRequest无法加载api / login。 No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'domain' is therefore not allowed access. 因此,不允许访问原始“域”。

<script type="text/javascript">


function ajax_trial() {

    var user_name="admin@abc.com";
    var passcode = "1233";
    var client_app_code = "1";
    var ip_address = "someIP";




    var json_string = '{"user_name" : "'+user_name+'","passcode":"'+passcode+'","client_app_code":"'+client_app_code+'","ip_address":"'+ip_address+'"}';

        alert(json_string);
    $.ajax({

        'accept': 'application/json',
        'contentType': 'application/json',
         type: "POST",
        dataType: "json",
        'data': json_string,
        cache: true,
        crossDomain: true,
        url: "/api/login",
        success: function(data) {
            alert("data "+data);
        },
        error: function(xhr, status, error) {
            alert(xhr.responseText);
        }
    });

}

i user a rest client and this is code i get when i send the json {"user_name":"admin@domain.com","passcode":"1233","client_app_code":"1","ip_address":"1323"} 我使用一个休息客户端,这是我发送json {“ user_name”:“ admin@domain.com”,“ passcode”:“ 1233”,“ client_app_code”:“ 1”,“ ip_address”:“时得到的代码1323“}

Request headers 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/31.0.1650.63 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,it;q=0.6
Response headers 
Server: Apache-Coyote/1.1
sessionId: 94c16db2-6bb8-11e3-a4e7-7845c4b4
Access-Control-Allow-Origin: *
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 23 Dec 2013 09:57:06 GMT 

I think you are sending an AJAX request to a different domain. 我认为您正在将AJAX请求发送到其他域。 That is, you are sending a request from your "localhost" to "something.com". 也就是说,您正在从“本地主机”向“ something.com”发送请求。 This is limited by the browsers due to Same Origin Policy . 由于同源策略,这受到浏览器的限制。

The web browser is blocking it as it usually allows request in the same origin for security reason. 网络浏览器将其阻止,因为出于安全原因,通常它允许来自同一来源的请求。

You'll need to do something different to do a cross-domain XHR. 您需要做一些不同的事情才能进行跨域XHR。 JSON-P is one way of achieving it. JSON-P是实现它的一种方法。

在克服开发过程中,您可以在浏览器中使用名为CORS的插件。

暂无
暂无

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

相关问题 加载失败 <resource> :所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Failed to load <resource>: No 'Access-Control-Allow-Origin' header is present on the requested resource No 'Access-Control-Allow-Origin' header is present on the requested resource error - No 'Access-Control-Allow-Origin' header is present on the requested resource error 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 - 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 请求的资源上不存在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 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource WordPress 请求的资源上不存在“Access-Control-Allow-Origin”标头 - WordPress No 'Access-Control-Allow-Origin' header is present on the requested resource 遇到请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头 - Encounter No 'Access-Control-Allow-Origin' header is present on the requested resource 所请求的资源上没有Access-Control-Allow-Origin&#39;标头 - NO Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM