【问题标题】:jQuery consuming JSON WCFjQuery 使用 JSON WCF
【发布时间】:2011-08-28 07:51:46
【问题描述】:

我有一个带有 3 个字符串参数的 WCF 服务。用 jQuery 和 JSON 调用它确实到达了我的方法,但只有 1 个参数包含一个值 - 其他参数被接收为 null,即使它们被传递。有什么想法吗?

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public string Save(string site, string title, string contentType)
    {
        // method...
    }

JS:

                    $.ajax(
                      {
                        type: "POST",
                        url: "/Service.svc/Save",
                        dataType: "json",
                        data: "{\"title\": \"title...\", \"site\": \"site...\", \"contentType\": \"contentType...\"}",
                        contentType: "application/json; charset=utf-8",
                        success: function(data) {
                        },
                        error: function() {
                          alert("Sorry, an error has occured");
                        }
                      }

【问题讨论】:

  • 您是否尝试过更正参数的顺序?您的服务方法接受订单站点、标题和内容类型中的参数,但您在订单标题、站点和内容类型中发送它。不确定这是否会解决它,但值得一试。
  • 是的,对不起,我应该提到这一点。参数的顺序不会改变结果。标题是唯一接收到值的参数。
  • 在我尝试时有效。我在 VS2010 中创建了一个 WCF 服务应用程序,我的 JQuery 版本是 1.5.1。这是一个预感:重命名站点和 contentType 名称。
  • 您是否在客户端对参数进行编码?

标签: c# jquery wcf json


【解决方案1】:

误报。

ServiceContract 中这两个缺失参数的参数名称与服务不同。我使用的是服务中的参数名称,而不是 ServiceContract 中的参数名称。愚蠢的。

谢谢大家的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2014-08-15
    • 1970-01-01
    • 2011-09-06
    • 2011-05-27
    相关资源
    最近更新 更多