我开发了一个 WCF Webservice,由 jQuery 调用。WCF Web 服务和启用 WCF Web 服务的 AJAX 客户端运行在不同的 Web 服务器上。
这是Web服务的定义。
[ServiceContract]
interface IPersonService
{
[OperationContract]
Person InsertPerson(Person person);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PersonService : IPersonService
{
...
[WebInvoke(UriTemplate = "/POST/PersonPost", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
public Person InsertPerson(Person person)
{
Debug.WriteLine("POST:[PersonId = {0} PersonName = {1}]", person.Id, person.Name);
return new Person(person.Id, person.Name);
}
}
[DataContract]
public class Person
{
[DataMember]
private string id;
[DataMember]
private string name;
public Person(string id, string name)
{
this.id = id;
this.name = name;
}
public string Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
var json = JsonConvert.SerializeObject(this);
return json.ToString();
}
}
这是客户端:
$.ajax({
type: "POST",
data: { "id": "Mehrere ;;; trennen", "name": "GetPerson" },
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processData: false,
crossDomain: true,
url: "http://localhost:59291/Person/POST/PersonPost",
success: function (data) {
alert("Post erfolgreich: ");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Fehler Post: Status " + xhr.status + " AntwortText " + xhr.responseText);
}
});
我得到 HTTP 代码 200。这还不错吧?但是有人可以告诉我,我怎样才能访问 jQuery 客户端发送到 WCF 服务的数据???
谢谢