0

I have developed a WCF Webservice, to be called by jQuery. The WCF Webservice and the AJAX Client which enables the WCF Webservice are running on different Webservers.

Here are the Definition of the Webservice.

[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();
    }
}

Here is the client:

$.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);
  }
});

I get HTTP Code 200. That is not bad? But can somebody tell me, how I can have access to the data which the jQuery Client send to the WCF Service???

Thanks

Higune
  • 643
  • 3
  • 13
  • 31

2 Answers2

0

if you are using jsonp datatype you must handel the json callback while returning the data...

jsonCallback(Your json response goes here);

ex:

      jsonCallback(
    {
        "sites":
        [
            {
                "siteName": "JQUERY4U",
                "domainName": "http://www.jquery4u.com",
                "description": "#1 jQuery Blog for your Daily News, Plugins, Tuts/Tips & Code Snippets."
            },
            {
                "siteName": "BLOGOOLA",
                "domainName": "http://www.blogoola.com",
                "description": "Expose your blog to millions and increase your audience."
            },
            {
                "siteName": "PHPSCRIPTS4U",
                "domainName": "http://www.phpscripts4u.com",
                "description": "The Blog of Enthusiastic PHP Scripters"
            }
        ]
    }
);
Dineshkumar
  • 351
  • 1
  • 8
  • Hi, thank you for your response: But I want read the data on the server, do you understand what I want. Or I am on the wrong sid of the street. – Higune Mar 21 '13 at 13:13
0
  1. The data for you JSON looks a little weird to me. I'm not a JSON expert. But if I were to write it, I'd have something like:

    var body = '{"person":{';
    body = body + '"id":"' + '"abc123"' + '",'
    body = body + '"name":"' + '"john"' + '"'
    body = body + '}}'; 
    

and then set the "data:" value equal to the "body" variable

            dataType: "json",
            data: body,

Note, this way I can add a debug line

alert(body);

in the code (after I create it) to see the data (before I send it via the request).

..........

Second.

Check my old conversation here:

CORS Support within WCF REST Services

Why?

Because I hit some issues, and when you had

"http://localhost:59291/"

that triggered that memory.

Community
  • 1
  • 1
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • Thank you, hm I have change my jQuery code. But this will not help me. Your second tip, well I don't know. I want just have the data from the jQuery client on my server. – Higune Mar 21 '13 at 13:50
  • Ok...my advice was to get the data TO the server first. Is that working? Or are you having trouble reading the data being sent back? – granadaCoder Mar 21 '13 at 13:52
  • I can't read the data, the server is working here I want have the data public Person InsertPerson(Person person) { Debug.WriteLine("POST:[PersonId = {0} PersonName = {1}]", person.Id, person.Name); return new Person(person.Id, person.Name); } – Higune Mar 21 '13 at 13:54
  • Is the code on the server side working correctly? Or not? "I can't read the data" is ambiguous. You have to be very clear on whether or not it is server side or client side you're having issues. Can you set a breakpoint on the server side, and do you ever get inside your code on the server side? – granadaCoder Mar 21 '13 at 13:55
  • Yes this is working, I came into the method, but the parameter person is null – Higune Mar 21 '13 at 13:56
  • Ok. That is a better description. Did you swap out to the "body" example like I suggested? Because you're calling the method, but if you don't encode the json correctly, the WCF service won't "get" the Person object. Also, I would change your method to "Person per"...to avoid confusion. The json wants the NAME of the ARGUMENT, not the data type. That is key. Note my "body" has lower case "person", not upper case. – granadaCoder Mar 21 '13 at 13:57
  • Yes that have i done, granadaCoder, I must go: If you have some other tips for me, this were great. I will check your tips tomorrow. – Higune Mar 21 '13 at 14:00
  • This probably doesnt matter. BUt I'd change the service and the concrete method signature to be Person InsertPerson(Person per), until I got it figured out. Then change the json to var body = '{"per":{'; ... just so it is clear. I'd check the CORS support thing as well. – granadaCoder Mar 21 '13 at 14:24
  • Hi granadaCoder, I have make the correction like you had sad to me. But it doesn't work. Now, I don't have a connection to the WCF Service method, but with fiddler I became a response, when I set the content-length to zero. – Higune Mar 22 '13 at 07:30