XMLHttpRequest无法加载:标题包含多个值' *,*,*',但只允许一个

时间:2016-12-14 00:56:49

标签: c# asp.net http cors wcf-rest

我有多个请求访问的问题。但我认为这只是一个请求。我不知道是什么问题。我创建项目WCF Rest然后将其调用到另一个项目中:

这是我的代码段。

IServiceRequest.cs

[OperationContract]
    [WebGet(
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/GetAllRequest/")]
    IEnumerable<USP_GET_DATA_Result> Get();

Web.Config中

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <!--add element connectionStrings-->
  <connectionStrings>
    <add name="Entities" connectionString="metadata=res://*/DBEntities.csdl|res://*/DBEntities.ssdl|res://*/DBEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.0.202;initial catalog=Example.KRIS;user id=sa;password=****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>
  <system.serviceModel>
    <!--add element extensions and services-->
    <extensions>
      <behaviorExtensions>
        <add name="crossOriginResourceSharingBehavior"
             type="WcfService.CORSEnablingBehavior, 
             WcfService, Version=1.0.0.0, Culture=neutral" />
      </behaviorExtensions>
    </extensions>
    <services>
      <service behaviorConfiguration="serviceBehavior" name="WcfService.ServiceRequest">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService.IServiceRequest" />
      </service>
    </services>
    <behaviors>
      <!--edit value-->
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <!--add element endpoint-->
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <crossOriginResourceSharingBehavior />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <!--edit protocolmapping-->
    <protocolMapping>
      <add binding="webHttpBinding" scheme="http" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization" />
      </customHeaders>
    </httpProtocol>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
&#13;
&#13;
&#13;

这是另一个调用WCF的项目

service.js

&#13;
&#13;
(function (app) {
    app.service("GetDataService", function ($http) {
        this.getRequestData = function () {
            return $http.get('/Approval/GetAll');
        };
    });
    app.service("CRUD_AngularJs_RESTService", function ($http) {
        this.getAllEntry = function () {
            return $http.get("http://localhost:51458/ServiceRequest.svc/GetAllRequest/");
        };
    });
})(angular.module('entry'));
&#13;
&#13;
&#13;

EntryCtrl.js

&#13;
&#13;
(function (app) {
    'use strict';

    app.controller('entryCtrl', entryCtrl);

    entryCtrl.$inject = ['$scope', 'CRUD_AngularJs_RESTService'];

    function entryCtrl($scope, CRUD_AngularJs_RESTService) {
        $scope.pageClass = 'page-entry';
        GetAllRecords();
        //To Get All Records
        function GetAllRecords() {
            var promiseGet = CRUD_AngularJs_RESTService.EntryData();
            promiseGet.then(function (pl) { $scope.Data = pl.data });
        }
    }
})(angular.module('entry'));
&#13;
&#13;
&#13;

这是我的问题有同样的请求enter image description here 这就是错误

enter image description here

我已经在我的项目WCF中添加了global.asax,例如:

&#13;
&#13;
protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE, XMODIFY");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }
&#13;
&#13;
&#13;

0 个答案:

没有答案