Với hợp đồng:Tiêu thụ WCF từ jQuery như JSON
namespace ACME.FooServices
{
[ServiceContract]
public interface IFooService
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
FooMessageType Foo(string name);
}
[DataContract]
public class FooMessageType
{
string _name;
string _date;
[DataMember]
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember]
public string Date
{
get { return _date; }
set { _date = value; }
}
}
}
Và thực hiện:
using System;
using System.ServiceModel.Activation;
namespace ACME.FooServices
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class FooService : IFooService
{
public FooMessageType Foo(string name)
{
string l_name = (String.IsNullOrWhiteSpace(name)) ? "Anonymous" : name;
return new FooMessageType {Name = l_name, Date = DateTime.Now.ToString("MM-dd-yyyy h:mm:ss tt")};
}
}
}
Configured trong web.config như:
<system.serviceModel>
<services>
<service name="ACME.FooServices.FooService">
<endpoint address="" behaviorConfiguration="ACME.FooBehaviour" binding="webHttpBinding" contract="ACME.FooServices.IFooService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="ACME.FooBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Tôi đang cố gắng để gọi Foo từ một trang qua jQuery:
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#msgButton").click(function() {
var params = {};
params.name = $("#nameTextbox").val();
$.ajax({
type: 'POST',
url: "http://acme.com/wcfsvc/FooService.svc/Foo",
data: JSON.stringify(params),
contentType: 'application/json; charset=utf-8',
success: function (response, status, xhr) { alert('success: ' + response); },
error: function (xhr, status, error) { alert("Error\n-----\n" + xhr.status + '\n' + xhr.responseText); },
complete: function (jqXHR, status) { alert('Status: ' + status + '\njqXHR: ' + JSON.stringify(jqXHR)); }
});
});
});
</script>
Nhưng tôi nhận được 400 - Yêu cầu không hợp lệ lỗi với thông báo "Máy chủ đã gặp lỗi khi xử lý yêu cầu. Thông báo ngoại lệ là 'Có lỗi khi deserializing đối tượng của kiểu System.String. Kết thúc phần tử 'root' từ không gian tên '' được mong đợi. Tìm thấy nguyên tố 'tên' từ namespace".
Tôi có thiếu cái gì?
tôi thu thập Bare và một DTO sẽ là một tốt nhất thực hành? – Bullines