通过JQuery进行WCF POST。如何在JSON中发送数组?

时间:2023-01-12 13:20:34

I want to send a JSON by POST to a WCF service using JQuery. The issue is that I really don't know how to send correctly this JSON with an array of objects so I get an 400 Bad Request.

我想通过POST将JSON发送到使用JQuery的WCF服务。问题是我真的不知道如何使用一组对象正确发送这个JSON,所以我得到一个400 Bad Request。

This is the JSON structure. As you can see, there're some fields and an array of files (name and its base64 body). The problem is the last part.

这是JSON结构。如您所见,有一些字段和一组文件(名称及其base64主体)。问题是最后一部分。

{
   "guid": "",
   "title": "d",
   "description": "d",
   "category": "19",
   "email": "emai@email.com",
   "priority": "1",
   "type": "2",
   "typeText": "Soli",
   "categoryText": "CU",
   "subCategoryText": "TMóvil",
   "files": [
      {
         "nameFile": "stack.txt",
         "fileContent": "data:text/plain;base64,Y2xvd24="
      }
   ]
}

this is the code which sends the JSON:

这是发送JSON的代码:

    $.ajax({
        url: serviceUrl,
        type: "POST",
        data: JSON.stringify(params),
        async: true, 
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (data) {.....
        error: function(data)....
})

This is my interface on the server side:

这是我在服务器端的界面:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "NewRequest", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
NewRequestResponse NewRequest(NewRequestTO obj);

And this is the NewRequestTO class

这是NewRequestTO类

 [DataContract]
    public class NewRequestTO
    {
        [DataMember]
        public string guid { get; set; }

        [DataMember]
        public string title { get; set; }

        [DataMember]
        public string description { get; set; }

        [DataMember]
        public string category { get; set; }

        [DataMember]
        public string email { get; set; }

        [DataMember]
        public string priority { get; set; } 

        [DataMember]
        public string type { get; set; }

        [DataMember]
        public string typeText { get; set; }

        [DataMember]
        public string categoryText { get; set; }

        [DataMember]
        public string subCategoryText { get; set; }

        [DataMember]
        public string files { get; set; }

    }

The question is, how can I handle this information? What structure does I have to use?

问题是,我该如何处理这些信息?我必须使用什么结构?

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

This:

这个:

"files": [
      {
         "nameFile": "stack.txt",
         "fileContent": "data:text/plain;base64,Y2xvd24="
      }
   ]

Will be equivalent to an IEnumerable of objects, where the object has a property nameFile and fileContent.

将等效于IEnumerable对象,其中对象具有属性nameFile和fileContent。

e.g.

例如

[DataMember]
public FileData[] files { get; set; }

public class FileData {
   [DataMember]
   public string nameFile { get; set;}
   [DataMember]
   public string fileContent { get; set; }
}

#1


1  

This:

这个:

"files": [
      {
         "nameFile": "stack.txt",
         "fileContent": "data:text/plain;base64,Y2xvd24="
      }
   ]

Will be equivalent to an IEnumerable of objects, where the object has a property nameFile and fileContent.

将等效于IEnumerable对象,其中对象具有属性nameFile和fileContent。

e.g.

例如

[DataMember]
public FileData[] files { get; set; }

public class FileData {
   [DataMember]
   public string nameFile { get; set;}
   [DataMember]
   public string fileContent { get; set; }
}