Reputation: 199
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.
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.
{
"guid": "",
"title": "d",
"description": "d",
"category": "19",
"email": "[email protected]",
"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:
$.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
[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.
Upvotes: 1
Views: 374
Reputation: 33857
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.
e.g.
[DataMember]
public FileData[] files { get; set; }
public class FileData {
[DataMember]
public string nameFile { get; set;}
[DataMember]
public string fileContent { get; set; }
}
Upvotes: 1