【问题标题】:POSTing JSON to WCF REST Endpoint将 JSON 发布到 WCF REST 端点
【发布时间】:2010-12-09 13:41:19
【问题描述】:

我正在为我的服务实施 PUT、POST 和 DELETE。但是每次我尝试将一些 json 发送到服务器时,都会收到错误“无法创建抽象类”。我通过DataContractJsonSerializer 运行我的对象实例、添加__type 字段并将其包装在{"obj": mydata} 中生成了我的请求数据。

我可以通过 DataContractJsonSerializer 运行它,它需要一个 BaseObj 并且它工作正常:

 {"__type":"RepositoryItem:http:\/\/objects\/","Insert_date":null,"Modified_date":null,"insert_by":null,"last_modify_user_id":null,"modified_by":null, "external_id":"1234","internal_id":54322,"school_id":45,"type_name":0, "vendor_id":57}

我的服务合同装饰有ServiceKnownType 属性,列表中包含RepositoryItemBaseObj

我正在使用 jquery 发布

        $.ajax({
            type: "POST",
            url: "http://localhost/slnSDK/service.svc/create/repositoryitem.json?t=" + token, 
            data: data, 
            success: function(result) {
                $("#input").html(result);
            },
            error: function(xhr, result, err) {
                $("#htmloutput").html(xhr.responseText);
            },
            dataType: "json",
            contentType: "application/json"
        });

我暴露了以下端点:

<OperationContract(Action:=Api2Information.Namespace & "createJson")> _
<WebInvoke(Method:="POST", _
           BodyStyle:=WebMessageBodyStyle.Bare, _
           RequestFormat:=WebMessageFormat.Json, _
           responseFormat:=WebMessageFormat.Json, _
           UriTemplate:="/create/{objType}.json?t={token}")> _
Function createJson(ByVal objType As String, ByVal obj As BaseObj, ByVal token As String) As Integer

以及以下对象(IBaseObj 被省略,因为它可以由其实现者推断)

<DataContract(Namespace:="http://objects/")> _
Public Class RepositoryItem : Inherits BaseObj

    ' members backing properties have been omitted.

    Public Sub New()
    ...

    <DataMember()> _
    Public Property type_name() As eType
    ...

    ' Override this to expose it as a property on the WebAPI
    <DataMember()> _
    Public Overrides Property internal_id() As Integer?
    ...

    <DataMember()> _
    Public Property external_id() As String
    ...

    <DataMember()> _
    Public Property vendor_id() As Integer
    ...

End Class

<DataContract(Namespace:="http://objects/")> _
<Serializable()> _
Public MustInherit Class BaseObj : Implements IBaseObj

    ' members backing properties have been omitted.

    <DataMember()> _
    Public Overridable Property insert_by() As String Implements IBaseObj.Insert_by
    ...

    <DataMember()> _
    Public Overridable Property Insert_date() As Nullable(Of Date) Implements IBaseObj.Insert_date
    ...

    <DataMember()> _
    Public Overridable Property modified_by() As String Implements IBaseObj.Modified_by
    ...

    <DataMember()> _
    Public Overridable Property Modified_date() As Nullable(Of Date) Implements IBaseObj.Modified_date
    ...

    <DataMember()> _
    Public Overridable Property last_modify_user_id() As Nullable(Of Integer) Implements IBaseObj.Last_modify_user_id
    ...

End Class

来自 POST 的提琴手输出:

POST http://localhost/slnSDK/service.svc/create/repositoryitem.json?t= HTTP/1.1
Host: localhost
Connection: keep-alive
Referer: http://localhost/apitest.html
Content-Length: 265
Origin: http://localhost
X-Requested-With: XMLHttpRequest
Content-Type: application/json
Accept: application/json, text/javascript, */*; q=0.01
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) RockMelt/0.8.36.79 Chrome/7.0.517.44 Safari/534.7
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=ywyooz45mi3c4d55h4ld4bec; x=lHOtsYBHvS/fKE7JQWzFTw==; y=XhfNVfYYQynJrIZ/odWFOg==

{"obj":{"__type":"RepositoryItem:http:\/\/objects\/","Insert_date":null,"Modified_date":null,"insert_by":null,"last_modify_user_id":null,"modified_by":null, "external_id":"1234","internal_id":54322,"school_id":45,"type_name":0, "vendor_id":57}}

您能提供的任何帮助都会很棒。谢谢!

【问题讨论】:

    标签: javascript json wcf rest


    【解决方案1】:

    信息量很大,但远程调试总是很困难,一些提示:

    删除了提琴手提示(我可以看到你正在使用它)

    在您的 ajax 帖子中:

        success: function(result) {
            $("#input").html(result);
        },
    

    您应该使用 result.d 来获取消息内容。

        success: function(result) {
            $("#input").html(result.d);
        },
    

    调试消息中的 insert_by 字段为 null,从片段来看,它看起来不像 null 是可接受的(作为 String?而不是作为 String)。

    【讨论】:

    • 我更新了我的帖子以反映这一点,但该 HTTP 请求是使用 Fiddler 捕获的。另外,感谢您对返回数据的提醒。
    • 更新答案,空字段有问题吗?
    • 嗯。我用空字符串字段中的值再次尝试,没有骰子。
    猜你喜欢
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多