I have some trouble with an WCF Service: This Service should communicate with an AJAX Client, which is running on a other Server. So, I have a crossDomain problem. I search and work 2 days but I don't find some solution, that is quite easy to understand.
When I use Fiddler, they could communicate with the server but only when set the content-length attribute to zero.
GET method are also working but PUTs never.
Here is some code for the orientation:
Server Methods:
Model:
[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; }
}
Serverinterface & Implementation:
[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");
}
and finally the Client
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);
}
});
Can someone tell me what can I do, to bring this to work: In short, currently I don't have a connection to the Server when I use my jQuery method.