我有一个带有一个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);
我有一个单独的数据访问层,可以在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编码正常,我无法找到错误的原因。