Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# jQuery将数据发布到WCF Webservice_C#_Jquery_Wcf_Web Services_Post - Fatal编程技术网

C# jQuery将数据发布到WCF Webservice

C# jQuery将数据发布到WCF Webservice,c#,jquery,wcf,web-services,post,C#,Jquery,Wcf,Web Services,Post,我开发了一个WCF Web服务,由jQuery调用。WCF Web服务和支持WCF Web服务的AJAX客户端在不同的Web服务器上运行 下面是Webservice的定义 [ServiceContract] interface IPersonService { [OperationContract] Person InsertPerson(Person person); } [AspNetCompatibilityRequirements(RequirementsMode

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

下面是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();
    }
}
以下是客户:

$.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服务的数据


谢谢

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

jsonCallback(Your json response goes here);
例:

  • 你的JSON数据在我看来有点奇怪。我不是JSON专家。但如果我要写的话,我会有这样的想法:

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

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

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

    第二

    在这里查看我以前的对话:

    为什么?

    因为我碰到了一些问题,当你

    "http://localhost:59291/"
    

    这触发了记忆。

    嗨,谢谢你的回复:但我想读取服务器上的数据,你明白我想要什么吗。或者我走错了方向。你试过IE和Firefox吗?一个给你一个错误,另一个没有?(我问的是一个具体的原因,不仅仅是钓鱼)。比如说http代码200,Chrome什么都不说。你明白了吗?为了将来的读者,让人们知道一个解决方案总是很好的。谢谢,嗯,我已经更改了我的jQuery代码。但这对我没有帮助。你的第二个建议,我不知道。我只想把jQuery客户端的数据放在我的服务器上。好的……我的建议是先把数据放到服务器上。这样行吗?或者您在读取发送回的数据时遇到问题吗?我无法读取数据,服务器正在这里工作。我想让数据public Person InsertPerson(Person Person){Debug.WriteLine(“POST:[PersonId={0}PersonName={1}]”,Person.Id,Person.Name);返回新的Person(Person.Id,Person.Name);}服务器端的代码是否正常工作?还是不?“我看不懂数据”是含糊不清的。您必须非常清楚您遇到的问题是服务器端还是客户端。你能在服务器端设置一个断点吗?你有没有在服务器端进入你的代码?是的,这是有效的,我加入了这个方法,但是参数person是空的
    alert(body);
    
    "http://localhost:59291/"