简体   繁体   中英

jQuery post data to WCF Webservice

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM