代码之家  ›  专栏  ›  技术社区  ›  SteveCl

使用jquery将对象传递给wcf服务

  •  2
  • SteveCl  · 技术社区  · 15 年前

    我知道这里也有同样的问题,但是尽管我读过,我还是遗漏了一些东西,希望你能帮上忙!

    使用由vs添加的wcf模板非常简单!

    接口:

    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat= WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    CompositeType GetDataUsingDataContract(CompositeType composite);
    

    实施:

    public CompositeType GetDataUsingDataContract(CompositeType composite){
      if (composite == null)
      {
          throw new ArgumentNullException("composite");
      }
      if (composite.BoolValue)
      {
          composite.StringValue += "Suffix";
      }
      return composite;  
    

    }

    javascript:

    $.ajax({
      type: "POST",
      url: "http://localhost:1545/Service1.svc/GetDataUsingDataContract",
      data: JSON.stringify(compType),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      processdata: true,
      success: function (msg) {
          $("#txtTest").val(message.BoolValue + " : " + message.StringValue);
      },
      error: function (xhr, errorMsg, thrown) {
          $("#error").html(xhr.responseText);
    }
    

    (});

    我得到的错误是: 服务器在处理请求时遇到错误。异常消息为'value不能为空。参数名称:composite。

    因此,该值不会被传递,当它到达WCF服务时为空。

    *注意:我已经尝试调用接受字符串并返回复合类型的方法,这很好。*

    THX提前为您提供帮助

    1 回复  |  直到 12 年前
        1
  •  0
  •   user3766054    12 年前

    下面是如何构造comptype以便 JSON.stringify() 将生成服务预期的内容:

    var compType = { composite: { StringValue: "MyString", BoolValue: true } };