Jquery 未在帖子中将数据发送到 WCF
大家好,WCF 遇到了一些问题。由于某种原因,我根本没有发送任何数据,或者收到 500 内部服务器错误...这意味着 WCF 不知道我要发送什么。如果我更改为 WrappedRequest,我可以访问该服务,但不会发送任何内容。谁能看到我做错了什么吗?这里是javascript
var data = { UserId: 2, Name: "test" };
$.ajax({
url: 'http://localhost:54900/MyService.svc/SaveName',
type: "POST",
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
data: $.toJSON(data),
error: function (data, textStatus, errorThrown) {
alert(errorThrown);
},
success: function (data, textStatus, jqXHR) {
alert('success');
}
});
[WebInvoke(Method = "POST", BodyStyle
= WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
发现的问题。必须删除enablewebscript并将其保留在webHttp中。这解决了问题。
Hi guys running into a bit of an issue with WCF. For some reason I'm not sending any data at all or I get 500 internal server error... which means WCF has no idea what I'm sending. If I change to WrappedRequest I can hit the service, just nothing is sent. Can anyone see what I'm doing wrong? Here is the javascript
var data = { UserId: 2, Name: "test" };
$.ajax({
url: 'http://localhost:54900/MyService.svc/SaveName',
type: "POST",
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
data: $.toJSON(data),
error: function (data, textStatus, errorThrown) {
alert(errorThrown);
},
success: function (data, textStatus, jqXHR) {
alert('success');
}
});
[WebInvoke(Method = "POST", BodyStyle
= WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
found the problem. Had to remove enablewebscript and leave it at webHttp. That fixed the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用“WebMessageBodyStyle.Bare”选项时,您需要以不同的方式将数据发送到 WCF 服务。
您需要在 url 末尾添加参数值,而不是 ajax 调用的日期,如下所示:
url: 'http://localhost:54900/MyService.svc/SaveName/parameters'。
仅供参考 - 只需添加值而不是参数名称。
您还需要向 WCF 服务上的方法添加另一个属性,这是 UriTemplate,其格式必须如下所示:
UriTemplate = "/theNameOfYourMethod/{firstNameOfYourParameter}/{second...}"
问候
You need to send your data to your WCF service from different way when you use the "WebMessageBodyStyle.Bare" option.
you need to add the value of your parameters at the end of your url instead of the date of your ajax call, something like this:
url: 'http://localhost:54900/MyService.svc/SaveName/parameters'.
FYI- just need to add the value not the name of your parameters.
also you need to add another property to your method on the WCF service, this is the UriTemplate, this must be formated like this:
UriTemplate = "/theNameOfYourMethod/{firstNameOfYourParameter}/{second...}"
Regards
对不起克里斯
错误地完成了。这不是故意的
Sorry Chris
Done by mistake. It was not intentional