1

1 つの GET メソッドと 1 つの POST メソッドを持つ WCF サービスがあります。GET はうまく機能します。

フロントエンドは、WCF の GET および POST メソッドへの jquery ajax 呼び出しを使用する単純な HTML ページです。

問題は、Bad Request 応答が返されることです。関連するすべての投稿を読みましたが、私が知る限り、すべてが順調に進んでいると思います。

ソース:

私の契約:

[OperationContract]
        [WebGet(UriTemplate = "schedule?week={week}",
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare)]
        [Description("Return the food schedule; specify the dates to include.")]
        WeeklySchedule GetSchedule(int week);

        [OperationContract]
        [WebInvoke(UriTemplate="writeweek", Method = "POST", 
                    RequestFormat=WebMessageFormat.Json,
                    ResponseFormat=WebMessageFormat.Json,
                    BodyStyle=WebMessageBodyStyle.Bare)]
        [Description("Write or Update a week schedule via json data with POST")]
        int WriteWeek(string jsonwk);

DBで書き込み/読み取りを行い、正常に動作する別のデータアクセスレイヤーがあります。

ajax呼び出しに関する私のページのソース:

    function ajaxGetWeek(wk)
    {
        $.ajax({
            cache: false,
            type: "GET",
            processData: false,
            url: 'http://appsrv01/food/svc/foodland.svc/schedule?week='+ wk,
            dataType: "json",
            success: function(data) {
                    GetWeekSucceeded(data);
            },
            error: function (xhr,status,error) {
                alert(error);
            }
        });

これは正常に動作し、json 形式を返し、フォームで解析できます。

不正なリクエストを返す POST ajax 呼び出し:

function SubmitFood() {
        var features = new Object();    // Create empty javascript object
        features["weeknumber"] = currentWeek;
        $("#TheForm textarea").each(function() {           // Iterate over inputs
            features[$(this).attr("name")] = $(this).val();  // Add each to features object
        });         
        var jsontxt = JSON.stringify(features);
        $.ajax({
            type: "POST",
            url: 'http://appsrv01/food/svc/foodland.svc/writeweek',
            data: jsontxt,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(postresult) {
                    WriteWeekSucceeded(postresult);
            },
            error: function (xhr,status,error) {
                alert(error);
            }
        });

    }

上記は次のようなものを返します。

{"weeknumber":24,"DishMon":"sdfadsf","DishTue":"asdfasd","DishWed":"fasdfa","DishThu":"sdfasdfas","DishFri":"dfasdf"}

VS ソリューションにブレークポイントを設定しましたが、メソッドを呼び出すことさえできません。

私のapp.config:

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

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="FoodLandService.FoodLandService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6969/FoodLand"/>
          </baseAddresses>
        </host>
        <endpoint address="" 
                  binding="webHttpBinding" 
                  contract="ContractsClass.IFoodLandService" 
                  behaviorConfiguration="Web"  />
        <endpoint address="mex" 
                  binding="mexHttpBinding" 
                  contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="Web">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

web.config:

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

  <system.web>
    <compilation debug="false" targetFramework="4.0" />
    <customErrors mode="Off"/>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

および foodland.svc:

<%@ ServiceHost Service="FoodLandService.FoodLandService"
     Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

私のサービスソース:

public class FoodLandService : ContractsClass.IFoodLandService
{
    public WeeklySchedule GetSchedule(int week)
    {
        FoodClassDB fd = new FoodClassDB();
        return fd.ReadWeekDB(week);
    }

    public int WriteWeek(string jsonwk)
    {
        FoodClassDB fd = new FoodClassDB();
        return fd.WriteWeekDB(jsonwk);
    }
}

json は正常にエンコードされており、エラーの理由が見つかりません。

4

0 に答える 0