WCF サービスに問題があります。このサービスは、他のサーバーで実行されている AJAX クライアントと通信する必要があります。だから、クロスドメインの問題があります。私は2日間検索して作業しましたが、解決策が見つかりません。これは非常に理解しやすいものです。
Fiddler を使用すると、サーバーと通信できましたが、content-length 属性をゼロに設定した場合のみでした。
GET メソッドも機能していますが、PUT は機能していません。
オリエンテーションのコードは次のとおりです。
サーバー メソッド:
モデル:
[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; }
}
サーバーインターフェースと実装:
[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, 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");
}
そして最後にクライアント
var person = '{"per":{';
person = person + '"id":"' + '"abc123"' + '",'
person = person + '"name":"' + '"john"' + '"'
person = person + '}}';
alert(person);
$.support.cors = true;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
data: person,
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);
}
});
これを機能させるために何ができるか教えてください。要するに、現在、jQueryメソッドを使用するときにサーバーに接続できません。