XMLHttpRequest无法加载

XMLHttpRequest无法加载

我收到一条错误消息,提示“ 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>

最佳答案

首先,在传递数据时使用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>

09-11 19:52