Web服务的Ajax调用返回400错误请求

Web服务的Ajax调用返回400错误请求

本文介绍了对WCF Web服务的Ajax调用返回400错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WCF Web服务Service1,其中只有一个方法Test()返回一个字符串.我已经将此Web服务部署在测试计算机上,但是当我尝试从浏览器调用Test方法时,我仅收到400 Bad Request错误.当我尝试通过AJAX GET请求调用该方法时,也会发生相同的情况.但是令人惊讶的是,通过WCFTestClient调用时,该方法返回正确的结果.

I have a simple WCF web service Service1 with just one method Test() which returns a string. I have deployed this web service on a test machine, but when I try to invoke the Test method from the browser I simply get a 400 Bad Request error. The same happens when I try to invoke the method through an AJAX GET request. But surprisingly, the method returns the correct result when invoked through the WCFTestClient.

这是代码:

[ServiceContract]
public interface IService1
{
    // This method can be called to get a list of page sets per report.
    [OperationContract]
    [WebGet]
    string Test();
}

public class Service1 : IService1
{
    public string Test()
    {
        return "test";
    }
}

这是我的AJAX请求:

This is my AJAX request:

 var serverUrl1 = 'http://' + baseUrl + ':86/Service1.svc';

 function GetTestString()
 {
        var methodUrl = serverUrl1 + "/Test";
        $.ajax({
    async: false,
                type: "GET",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
            url: methodUrl,
                beforeSend: function (XMLHttpRequest) {
        //ensures the results will be returned as JSON.
              XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
    success: function (data) {
        ShowQRGSlides(data.d);
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
        alert("ERROR: GetAvailablePages() Check your browsers javascript console for more details." + " \n XmlHttpRequest: " + XmlHttpRequest + " \n textStatus: " + textStatus + " \n errorThrown: " + errorThrown);
    }
  });

 }

这是我的Web服务的web.config文件:

Here is the web.config file for my web service:

<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"/>
</system.web>
<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>

这只是自动生成的简单web.config.我无法弄清楚为什么无法通过浏览器或Ajax访问这种简单的Web服务方法.通过WCFTestClient访问时,相同的方法将返回结果.

This is just the simple web.config which gets automatically generated. I am unable to figure out why this simple web service method is not accessible through the browser or ajax. The same method returns the result when accessed through the WCFTestClient.

任何输入将不胜感激!谢谢.

Any inputs will be greatly appreciated! Thanks.

推荐答案

您需要将service部分添加到web.config文件中.除非您告诉主机,否则主机不知道您要使用 webHttpBinding .

You need to add service section to your web.config file. Host does not know that you want to use webHttpBinding unless you tell him.

<services>
  <service name="Service1">
    <endpoint address=""
              binding="webHttpBinding"
              contract="IService1" />
  </service>
</services>

下面的链接提供了在IIS中托管服务的详细说明(使用 wsHttpBinding ).您只需要使用 webHttpBinding 而不是 wsHttpBinding - http://msdn.microsoft.com/en-us/library/ms733766.aspx

Link below provides detailed instructions for hosting service in IIS (with wsHttpBinding). You just need to use webHttpBinding instead of wsHttpBinding -http://msdn.microsoft.com/en-us/library/ms733766.aspx

这篇关于对WCF Web服务的Ajax调用返回400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:52