0

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.

Higune
  • 643
  • 3
  • 13
  • 31

1 Answers1

0

First of all you should use an object:

var myObject ={};
myObject.Id = '';
myObject.Name ='';

and use JSON.stringify(myObject);

May want to remove this from your ajax call : dataType: "jsonp", also you may want to check the problem from this question Cross domain ajax request.

    var person = {};
person.id ="abc123";
person.name ="aaaa";
var per = {};
per.per = person;

                $.support.cors = true;

                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(per),
                    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);
                    }
                });
Community
  • 1
  • 1
radu florescu
  • 4,315
  • 10
  • 60
  • 92
  • Fehler Post: Status 200 AntwortText undefined – Higune Mar 22 '13 at 10:56
  • I take .NET 4.5, do you know how I could enable CORS in the Visual Studion debugging – Higune Mar 22 '13 at 11:00
  • you are are the men, now I have access to my method in the server. But how can I have access to my Person object, which the client sent by the data – Higune Mar 22 '13 at 11:08
  • one minute Floradu88, can you perhaps say how could I have access to the objects/data in the Servermethod – Higune Mar 22 '13 at 11:10
  • there is still the jsonp for the datatype but is that correct? and I think the access to the server still work in the IE not in chrome. but first i check your tip – Higune Mar 22 '13 at 11:14
  • it doesn't work: it is very strange in the IE the method will started, but there is the per object NULL, and via Chrome, there can I don't start the method – Higune Mar 22 '13 at 11:17
  • it is working like before, in the server method I don't have access to the data: did you know why the IE is accepting the jQuery Script and Chrome not – Higune Mar 22 '13 at 11:25
  • do you think my insertPerson method is correct, or must I there make some changes – Higune Mar 22 '13 at 11:27
  • I don't think so, where can I check this – Higune Mar 22 '13 at 11:30
  • f12 on chrome and check console tab. provide a prinscreen in the question if there is something with red. – radu florescu Mar 22 '13 at 11:30
  • XMLHttpRequest cannot load http://localhost:59291/Person/POST/PersonPost. Origin http://ogwnanqvtest is not allowed by Access-Control-Allow-Origin. – Higune Mar 22 '13 at 11:31
  • what, I don't understand you; you are very friendly;) – Higune Mar 22 '13 at 11:35
  • do you have still some tips for me – Higune Mar 22 '13 at 11:45
  • put this back: dataType: "jsonp", if this does not work then you will need to screen share for more tips. – radu florescu Mar 22 '13 at 11:52
  • check my previous comment out – radu florescu Mar 22 '13 at 12:02
  • then I get this error: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://localhost:59291/Person/POST/PersonPost?callback=jQuery19103718682825…&{%22per%22:{%22id%22:%22abc123%22,%22name%22:%22aaaa%22}}&_=1363954689947 – Higune Mar 22 '13 at 12:19
  • and the IE doesn't activate the method in the server – Higune Mar 22 '13 at 12:20
  • check this question out, http://stackoverflow.com/questions/14051432/the-server-responded-with-a-status-of-405-method-not-allowed – radu florescu Mar 22 '13 at 12:27
  • the suggestion they made is to include the jQuery client into the main project but this is not what i can do – Higune Mar 22 '13 at 12:34