我想將JSON數據發送到我的WCF服務,但在服務中,我的對象始終爲空。我閱讀過很多文章,但是我無法解決我的問題。WCF發送JSON,方法中的對象始終爲空
[WebInvoke(UriTemplate = "/POST/PersonPost", Method = "POST",BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public Person InsertPerson(Person per)
{ Debug.WriteLine("InsertPerson");
if (per == null)
{
return new Person("2", "null");
}
Debug.WriteLine("POST:[PersonId = {0} PersonName = {1}]", per.Id, per.Name);
return new Person("1", "InsertPerson");
}
[DataContract]
public class Person
{
public Person(string id, string name)
{
this.id = id;
this.name = name;
}
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
public override string ToString()
{
var json = JsonConvert.SerializeObject(this);
return json.ToString();
}
}
,在這裏我的jQuery:
var person = {};
person.Id = "abc123";
person.Name = "aaaa";
var per = {};
per.per = person;
var param = JSON.stringify(per);
//param = "{"per":{"Id":"abc123","Name":"aaaa"}}"
$.support.cors = true;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: param,
dataType: "json",
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);
}
});
有什麼錯?爲什麼我的參數在insertPerson方法中總是爲null。
您的服務和網站是否在同一個域中運行?如果沒有,這將是因爲這個http://en.wikipedia.org/wiki/Same_origin_policy。您無法通過域發送請求。瀏覽器不會讓你。那麼不是沒有配置.. – Liam 2013-03-22 14:16:33
沒有他們在兩個不同的地方運行,但我不能改變這一點。服務器端的方法正在啓動,參數只有null。沒有辦法嗎? – Higune 2013-03-22 14:18:27
是我的配置,因此是正確的,我的意思是與我的服務兼容的JSON – Higune 2013-03-22 14:23:04