ASP正确配置允许asmx webservice中的CORS用于POST请求

时间:2016-11-03 16:47:08

标签: web-services web-config cors polymer-1.0

我正在尝试设置一个能够处理CORS的服务器,但也能够以json格式返回响应。 试过各种配置方案,但没有运气。

我需要通过POST做我的Ajax请求。使用我的实际配置,我可以通过GET进行请求,但目前我的响应始终以XML格式封装。

ResponseFormat.Json returns xml中所述,我的请求必须是'application / json',但当我尝试以这种方式执行此操作时,我收到预检消息(预检的响应具有无效的HTTP状态代码404)。

我的web.config(允许通过GET进行CORS)目前是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2">
      <assemblies>
        <add assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.Shared, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <!--<add assembly="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>-->
        <add assembly="CrystalDecisions.ReportSource, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.ReportAppServer.Controllers, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.ReportAppServer.DataDefModel, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.CrystalReports.Engine, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304" />
        <add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
      </assemblies>
    </compilation>
    <httpRuntime />
    <pages controlRenderingCompatibilityVersion="4.0" />
    <httpHandlers>
      <add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
    </httpHandlers>
    <sessionState timeout="30"></sessionState>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
  </system.web>
  <appSettings>  
    <add key="CrystalImageCleaner-AutoStart" value="true" />
    <add key="CrystalImageCleaner-Sleep" value="60000" />
    <add key="CrystalImageCleaner-Age" value="120000" />
  </appSettings>
  <connectionStrings>
    <....>
  </connectionStrings>
  <system.webServer>
    <handlers>
      <add name="CrystalImageHandler.aspx_GET" verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" /><remove name="OPTIONSVerbHandler" /><remove name="TRACEVerbHandler" /><add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /></handlers>
    <validation validateIntegratedModeConfiguration="false" />
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="X-Requested-With, Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

我正在使用铁聚合物来处理Ajax请求并组合各种内容类型。目前我通过GET使用'content-type =“application / x-www-form-urlencoded; charset = UTF-8”handle-as =“document”'来获取我的数据。

由于这种XML封装,我很难处理响应。以下是收到回复的两个例子。如何提取内容?

1:

<?xml version="1.0" encoding="utf-8"?>
        <string xmlns="http://my.domain/">EF7B48E346BC537F98F9696304CECAFE</string>

2:

'<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCiudad xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://my.domain/">
  <Ciudad>
    <Codigo>32</Codigo>
    <Nombre>BOGOTA</Nombre>
    <CodigoDepartamento>Z03</CodigoDepartamento>
  </Ciudad>
  <Ciudad>
    <Codigo>133</Codigo>
    <Nombre>CALI</Nombre>
    <CodigoDepartamento>Z01</CodigoDepartamento>
  </Ciudad>
  <Ciudad>
    <Codigo>53</Codigo>
    <Nombre>CARTAGENA</Nombre>
    <CodigoDepartamento>Z13</CodigoDepartamento>
  </Ciudad>
  <Ciudad>
    <Codigo>30</Codigo>
    <Nombre>MEDELLIN</Nombre>
    <CodigoDepartamento>Z04</CodigoDepartamento>
  </Ciudad>
</ArrayOfCiudad>'

0 个答案:

没有答案