不允许跨源请求阻止或方法

时间:2015-08-18 13:15:06

标签: c# wcf cors

从jQuery Ajax访问WCF RESTFul服务时遇到问题。整个情况正在解释如下:

WCF服务

[WebInvoke(Method="POST", UriTemplate="AllUsers",  ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
public string showUsersList(int userStat)
{
   return "Hi Agnib";
}

WCF web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime  maxRequestLength="5242880"  />
  </system.web>

<bindings>
  <basicHttpBinding>
    <binding name="ServicesBinding"
             maxReceivedMessageSize="2147483647"
    maxBufferSize="2147483647" sendTimeout="00:10:00" >
      <readerQuotas
            maxArrayLength="2147483647"
            maxBytesPerRead="2147483647"
      maxDepth="2147483647"
      maxNameTableCharCount="2147483647"
      maxStringContentLength="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>


<services>
  <service name="VMS_WCF_Service.Service1" behaviorConfiguration="VMS_WCF_Service.VMSBehavior">
    <endpoint address="Admin.svc" binding="basicHttpBinding"  contract="VMS_WCF_Service.IAdmin"   bindingConfiguration="ServicesBinding" >
      <identity>
        <dns value="xxx.xxx.xx.xx"/>
      </identity>
    </endpoint>

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
  </service>
  <service  name="VMS_WCF_Service.Company"  behaviorConfiguration="VMS_WCF_Service.VMSBehavior"  >

    <endpoint address="Company.svc"   binding="basicHttpBinding"   contract="VMS_WCF_Service.ICompany"   bindingConfiguration="ServicesBinding" >
      <identity>
        <dns value="xxx.xxx.xx.xx"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
  <service  name="VMS_WCF_Service.Assignments"   behaviorConfiguration="VMS_WCF_Service.VMSBehavior"  >

    <endpoint address="Assignments.svc"   binding="basicHttpBinding"   contract="VMS_WCF_Service.IAssignments"   bindingConfiguration="ServicesBinding" >
      <identity>
        <dns value="xxx.xxx.xx.xx"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>

  <service name="VMS_WCF_Service.Admin_UserDet" behaviorConfiguration="JSONBehavior">
    <endpoint name="REST" address="" binding="webHttpBinding" contract="VMS_WCF_Service.IAdmin_UserDet" behaviorConfiguration="RESTBHV"></endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
  </service>

</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="VMS_WCF_Service.VMSBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
    <behavior name="JSONBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>

  <endpointBehaviors>
    <behavior name="RESTBHV">
      <webHttp/>
    </behavior>
  </endpointBehaviors>

</behaviors>

<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"   multipleSiteBindingsEnabled="true" />
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<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>
</system.webServer>
</configuration>

我的网络应用程序(frmAdminModule.aspx)

<%@ Page Title="" Language="C#" MasterPageFile="~/VMSSite.Master"  AutoEventWireup="true" CodeBehind="frmAdminModule.aspx.cs" Inherits="VMS.frmAdminModule" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript" src="Scripts/ajaxOperations.js"></script>

<link type="text/css" rel="stylesheet" href="Content/css/bootstrap.css" />
<script type="text/javascript" src="Scripts/bootstrap.min.js"></script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ChildContent" runat="server">

**<HTML CODE GOES HERE>**

</asp:Content>

母版页面已包含以下脚本:

<script type="text/javascript" src="Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript" src="Scripts/jquery-ui-1.10.4.custom.js"></script>
<script type="text/javascript" src="Scripts/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript" src="Scripts/JScript.js"></script>

frmAdminModule.aspx.cs

if (!IsPostBack)
        {
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "UsersList", "showActiveUsers();", true);
        }

ajaxOperations.js

function showActiveUsers() {
alert("a");
$.ajax({
    type: "post",
    url: "<WCF Url>/AllUsers",
    data: "{}",
    dataType: "json",
    contentType: "application/json; utf-8",
    success: function (resp) {
        $('#respVal').html(resp.d);
    },
    error: function (xhr, errorType, exception) {
        alert(errorType);
        alert(exception);
    }
});

}

在运行我的网络应用程序时,网址为:http://localhost:50941/frmAdminModule.aspx;和

WCF网址是: http://localhost:49541/Admin_UserDet.svc

在Firefox中,它显示警告信息 -

阻止跨源请求:同源策略禁止在/ AllUsers读取远程资源。这可以通过将资源移动到同一域或启用CORS来解决。

在Chrome中显示 -

OPTIONS / AllUsers

XMLHttpRequest无法加载/ AllUsers。无效的HTTP状态代码405

我甚至在WCF Web.config文件中放置了启用CORS的代码(如上所述)。但是,它仍然不起作用,我无法理解其中的原因。

请帮帮我们。

感谢。

0 个答案:

没有答案