我有一个带有一个 GET 和一个 POST 方法的 WCF 服务。GET 运行良好。
前端是一个简单的 HTML 页面,它使用 jquery ajax 调用 WCF 的 GET 和 POST 方法。
问题是我收到了错误的请求响应。我已阅读所有相关帖子,据我所知,我认为一切都井井有条。
资料来源:
我的合同:
[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);
我有一个单独的数据访问层,它在数据库中进行写入/读取并且工作正常。
我关于 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>
网络配置:
<?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 编码很好,我找不到错误的原因。