【问题标题】:XMLHttpRequest cannot load [url] Response for preflight has invalid HTTP status code 400XMLHttpRequest 无法加载 [url] 预检响应具有无效的 HTTP 状态代码 400
【发布时间】:2015-09-25 14:10:21
【问题描述】:

我收到一条错误消息“XMLHttpRequest cannot load [url] Response for preflight has invalid HTTP status code 400.

我尝试从表单调用,它似乎正在工作。我从 Visual Studio 内部调试了服务,它工作得很好

以这种方式调用服务:

                    $.ajax({
                        type: "POST",
                        url: "http://localhost:54664/PopulateCombo.svc/GetCodigo",
                        data: { EmpresaId: 100100, LanguageId: 5, TipoId: TipoId },
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            var models = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
                            for (var i = 0; i < models.length; i++) {
                                var val = models[i];
                                var text = models[i];
                                $('#ddValor').addOption(val, text, false);
                            }
                        }
                    });

我的网络配置。

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

              <system.serviceModel>
                <bindings>
                  <basicHttpBinding>
                    <binding name="BasicHttpBinding_IPopulateCombo" sendTimeout="00:05:00" />
                    <binding name="BasicHttpBinding_IPopulateCombo1" />
                  </basicHttpBinding>
                </bindings>
                <client>

                  <endpoint address="http://localhost:54664/PopulateCombo.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPopulateCombo1"
                    contract="ACPSvc.IPopulateCombo" name="BasicHttpBinding_IPopulateCombo1" />
                </client>
              </system.serviceModel>

【问题讨论】:

    标签: asp.net vb.net wcf


    【解决方案1】:

    首先,在传递数据时使用 JSON.stringify 函数:

    data: JSON.stringify({ EmpresaId: 100100, LanguageId: 5, TipoId: TipoId }),
    

    接下来,使用 webHttp 端点行为:

    <bindings>
        <webHttpBinding>
            <binding name="BasicHttpBinding_IPopulateCom" sendTimeout="00:05:00" />
            <binding name="BasicHttpBinding_IPopulateCombo1" />         
        </webHttpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior name="WebHTTPBehavior">
              <webHttp helpEnabled="true"/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
        <endpoint address="http://localhost:54664/PopulateCombo.svc" binding="webHttpBinding" contract="ACPSvc.IPopulateCombo" behaviorConfiguration="WebHTTPBehavior"/>
    </services>
    

    【讨论】:

    • 请考虑改进您的答案,包括对问题原因的分析和对您提出的解决方案可能有效的原因的描述。
    【解决方案2】:

    我在我的 web.config 中添加了这个,它解决了这个问题(碰到另一个问题)

    <serviceHostingEnvironment>
      <serviceActivations>
        <add factory="System.ServiceModel.Activation.WebServiceHostFactory"
             relativeAddress="./ACPWebSvc/PopulateCombo.svc"
             service="PopulateCombo"/>
      </serviceActivations>
    </serviceHostingEnvironment>
    

    【讨论】:

    • 请考虑改进您的答案,包括对问题原因的分析和对您提出的解决方案似乎有效的原因的描述。
    猜你喜欢
    • 2016-03-27
    • 2016-04-10
    • 2017-04-13
    • 2016-09-19
    • 2016-11-14
    • 2016-08-03
    • 2016-06-30
    • 2016-08-24
    • 2016-01-18
    相关资源
    最近更新 更多