0

重複の可能性:
Javascript を使用して呼び出すと、WCF Web サービスが「Bad Request」エラーを返す

文字列を返すメソッド Test() を 1 つだけ持つ単純な WCF Web サービス Service1 があります。この Web サービスをテスト マシンにデプロイしましたが、ブラウザーから Test メソッドを呼び出そうとすると、単に 400 Bad Request エラーが発生します。AJAX GET リクエストを介してメソッドを呼び出そうとすると、同じことが起こります。しかし驚くべきことに、このメソッドは 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リクエストです:

 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ファイルは次のとおりです。

<?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 です。この単純な Web サービス メソッドがブラウザーまたは ajax からアクセスできない理由がわかりません。WCFTestClient を介してアクセスすると、同じメソッドが結果を返します。

どんな入力でも大歓迎です!ありがとう。

4

1 に答える 1

1

サービス セクションを web.config ファイルに追加する必要があります。あなたが彼に言わない限り、ホストはあなたが使いたいと思っていることを知りませんwebHttpBinding

<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

于 2012-06-13T20:24:13.400 に答える