当前位置: 首页 > 知识库问答 >
问题:

(原因:缺少CORS标头'Access-Control-Allow-Origine)

邓元白
2023-03-14

大家好,我有一个问题,我不能从客户机获得JSON值。我已经创建了WCF REST服务,并在客户机的IIS中添加了该服务。从客户机,打开我的网站,这个方法readIndicator被触发。但是收到了一个错误消息。我已经在web.config添加了Access-Control-Allog-Origin。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/weightservice/WeightService.svc/GetReader. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

WCF web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>  
    <add key="BandRate" value="2400"/>  
    <add key="Parify" value="0"/>
    <add key="Port" value="COM3"/>  
    <add key="Bits" value="8"/>
    <add key="Model" value="BDI2001B"/>  
    <add key="DecimalPoint" value="0"/>  
    <add key="Output" value="BDI-2001"/>  
  </appSettings>
  <connectionStrings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <!--
        The <authentication> section enables configuration 
        of the security authentication mode used by 
        ASP.NET to identify an incoming user. 
    -->
    <authentication mode="Windows"/>
    <!--
        The <customErrors> section enables configuration 
        of what to do if/when an unhandled error occurs 
        during the execution of a request. Specifically, 
        it enables developers to configure html error pages 
        to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
         <error statusCode="403" redirect="NoAccess.htm" />
         <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
    -->
    <authorization>
      <allow users="*"/>
    </authorization>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
  <!-- 
      The system.webServer section is required for running ASP.NET AJAX under Internet
      Information Services 7.0.  It is not necessary for previous version of IIS.
  -->
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
        <add name="Access-Control-Max-Age" value="1728000" />
      </customHeaders>
    </httpProtocol>
    <!--
        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>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors >
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="web">

          <webHttp/>

        </behavior>

      </endpointBehaviors>

    </behaviors>
    <services>
      <service name="WCFWeightService.WeightService" behaviorConfiguration="ServiceBehavior">

        <endpoint binding="webHttpBinding" contract="WCFWeightService.IWeightService" behaviorConfiguration="web">


        </endpoint>
      </service>

    </services>

    <serviceHostingEnvironment  multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>
function readIndicator() {
    try {
        $.ajax({
            type: "GET",
            dataType: 'json',
            async: true,
            contentType: "application/x-www-form-urlencoded",
            url: 'http://localhost/weightservice/WeightService.svc/GetReader',
            success: function (data) {
                if (data != "" || data != null) {
                    console.log(data);
                    console.log(data.GetReaderResult);
                    $("#indicator").text(data.GetReaderResult);
                }
            },
            error: function (request, status, error) {
                //gmyMsgbox('Failed to perform CheckToyNameExist', 'Err');
                //err.preventDefault();
                console.log(error);
            },
        });
    } catch (err) {
        //gmyMsgbox(err.message, 'Err');
        console.log(err);
        err.preventDefault();
    }
}

我已经用我自己的电脑试过了,没问题。我不知道我犯了什么错误。我希望你们能帮我解决这个问题,提前谢谢。

共有1个答案

钱毅
2023-03-14

现在,我只是在客户端PC浏览器上安装了插件“允许CORS:访问-控制-允许-起源”。

 类似资料: