XMLHttpRequest无法加载[url]预检的响应具有无效的HTTP状态代码400

时间:2015-09-25 14:10:22

标签: asp.net vb.net wcf

我收到错误消息“XMLHttpRequest无法加载[url]预检的响应包含无效的HTTP状态代码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>

2 个答案:

答案 0 :(得分:-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>

答案 1 :(得分:-1)

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

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