jQuery将数据发布到WCF Web服务

时间:2013-03-21 13:04:37

标签: c# jquery wcf web-services post

我开发了一个由jQuery调用的WCF Web服务。启用WCF Web服务的WCF Web服务和AJAX客户端在不同的Web服务器上运行。

以下是Web服务的定义。

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

这是客户:

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

我收到HTTP代码200.那还不错?但有人可以告诉我,我如何能够访问jQuery客户端发送给WCF服务的数据?

由于

2 个答案:

答案 0 :(得分:0)

如果您使用的是jsonp数据类型,则必须在返回数据时对json回调进行处理...

jsonCallback(Your json response goes here);

例如:

      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 :(得分:0)

  1. JSON的数据对我来说有点奇怪。我不是JSON专家。但如果我要写它,我会有类似的东西:

    var body = '{"person":{';
    body = body + '"id":"' + '"abc123"' + '",'
    body = body + '"name":"' + '"john"' + '"'
    body = body + '}}'; 
    
  2. 然后将“data:”值设置为等于“body”变量

                dataType: "json",
                data: body,
    

    注意,这样我可以添加调试行

    alert(body);
    

    在代码中(在我创建之后)查看数据(在我通过请求发送之前)。

    ..........

    第二

    在这里检查我的旧会话:

    CORS Support within WCF REST Services

    为什么?

    因为我遇到了一些问题,而且当你遇到了

    "http://localhost:59291/"
    

    触发了那段记忆。

相关问题