AJAX POST到WCF web配置需要另一套眼睛/头脑



由于某些原因,我无法在我的web服务(svc(中获得一个非常简单的AJAX帖子。我确信我的web.config有问题,但我尝试了所有我能在网上找到的东西。

在";MyService.svc";如果是使用

[OperationContract]
[WebInvoke(Method ="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat  =WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Wrapped)]
public string DoWork()
{
string result = "did it";
//result = test;
return (new JavaScriptSerializer().Serialize(result)); ;
}

如果DoWork只返回一个值,Ajax就会成功。但是,如果我将参数传递给DoWork(字符串测试(Ajax返回一个错误。错误的请求。

我的ajax是:

function DoTest() {
testData = { "test": "Hello" };
var jsonData = JSON.stringify(testData);
var POSTURL = "MyService.svc/DoWork";
//alert(jsonData);
//alert(POSTURL);
$.ajax({
type: "POST",
url: POSTURL,
datatype: "json",
data: jsonData,
ContentType: "application/json; charset=utf-8",
success: function (result) {
alert("success: " + result.d);
},
error: function (xhr, status, error) {
alert("Opps: " + xhr + " " + status + " " + error);
}
})
}
});

这在我的web.config中:

<behaviors>
<endpointBehaviors>
<behavior name="MyServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" behaviorConfiguration="MyServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="MyService" />
</service>
</services>
</system.serviceModel>

我需要另一双眼睛来观察这个原本简单的程序。感谢

"坏请求";通常是请求格式中的错误。您可以使用WCF中的帮助文档来查看请求格式。如果您想启用帮助文档,您需要将WebMessageBodyStyle更改为bare:

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Bare)]

然后启用配置文件中的帮助文档:

<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>

在浏览器中查看请求的格式,您需要以其格式发送数据

我删除了一个多余的<端点地址="我的复习中有。现在正在从AJAX进行服务调用。Phew。这个WCF在工作时工作得很好,在不工作时很难诊断。感谢聆听,祝大家好运。

最新更新