简体   繁体   English

Angular JS的跨域请求被阻止

[英]Cross-Origin Request Blocked for Angular JS

I am getting following error in the Angular JS. 我在Angular JS中遇到以下错误。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.1.28:3000/system/organizations/logos/000/000/001/original/img-user.png?1453890721. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

I search about the solution and I found solution like 我搜索解决方案,发现解决方案如

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

This above code, we need to add in - Web.Config 上面的代码,我们需要添加-Web.Config

Now, I have simple angular application, I not able to understand where is Web.config is there and where should I create it? 现在,我有一个简单的角度应用程序,我无法理解Web.config在哪里以及应该在哪里创建?

Can you please help me for - what should I do for the solution? 您能帮我吗-解决方案该怎么办?

Thanks, In advance. 提前致谢。

The file web.config should be a part of your server application (ASP.NET) which can customize the way it(the server) behaves. 文件web.config应该是服务器应用程序(ASP.NET)的一部分,该服务器应用程序可以自定义它(服务器)的行为方式。

Web.config is the main settings and configuration file for an ASP.NET web application. Web.config是ASP.NET Web应用程序的主要设置和配置文件。 It is an XML document that resides in the root directory of the site or application and contains data about how the web application will act. 它是一个XML文档,位于站点或应用程序的根目录中,并包含有关Web应用程序如何工作的数据。

The code you have mentioned in your question tells the server application that it should include Access-Control-Allow-Origin header in its response. 您在问题中提到的代码告诉服务器应用程序,它的响应中应包括Access-Control-Allow-Origin标头。

Your angular app then receives it and process the rest normally. 然后,您的角度应用程序将收到它并正常处理其余部分。

Please note that this is a server side solution - nothing to do with your client side angular script. 请注意,这是服务器端解决方案-与您的客户端角度脚本无关。

Here is a tutorial about web.config . 这是有关web.config的教程。

I am not sure that the proposed solution that you mentioned will work with client side (angular application) . 我不确定您提到的建议解决方案是否可以与客户端(角度应用程序)一起使用。

You will need to configure the mentioned solution on server side. 您将需要在服务器端配置上述解决方案。 With client side (from your browser) you may need to send some attributes that you can configure in your angular application. 使用客户端(从浏览器)时,您可能需要发送一些可以在angular应用程序中配置的属性。 You can configure the properties with the help of $httpProvider. 您可以在$ httpProvider的帮助下配置属性。 As per angular js documentation 根据Angular JS文档

Use $httpProvider to change the default behavior of the $http service. 使用$ httpProvider更改$ http服务的默认行为。 So we can use this service and can set the properties that will be passed with every request. 因此,我们可以使用此服务并可以设置将随每个请求传递的属性。 Most likely you will need to configure these setting 您很可能需要配置这些设置

Here I am configuring this in the main app.js file which defines our application named as app 在这里,我在主app.js文件中进行配置,该文件定义了名为app的应用程序

(function () {
    'use strict';

angular.module('app', []).config(appConfig).run(appRun)

appRun.$inject = ['$rootScope','$location','$state','$stateParams', '$log'];

appConfig.$inject = ['$httpProvider'];

/**
 * To give http $httpProvider default values
 * @param $httpProvider
 */
function appConfig($httpProvider){
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";
}


function appRun ($rootScope, $location, $state, $stateParams, $log) {
    console.info('Inside appRun');
}
})();

Hope this be of some help. 希望这会有所帮助。 Happy Learning :) 快乐学习:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM