I'm a beginner in a Knockout.js and I'm having a problem with sending data to the server. On my back-end I have REST-service:
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/smth")
public Response smth(MyObj[] values) {
return Response.ok().build();
}
Where MyObj object is:
@XmlRootElement
public class MyObj {
@XmlAttribute
private String linkName;
@XmlAttribute
private String linkedIssueKey;
public MyObj (String linkName, String linkedKey) {
this.linkName = linkName;
this.linkedIssueKey = linkedKey;
}
public MyObj () {
}
public String getLinkName() {
return linkName;
}
public String getLinkedIssueKey() {
return linkedIssueKey;
}
public void setLinkedIssueKey(String linkedIssueKey) {
this.linkedIssueKey = linkedIssueKey;
}
public void setLinkName(String linkName) {
this.linkName = linkName;
}
@Override
public String toString() {
return "MyObj{" +
"linkName='" + linkName + '\'' +
", linkedIssueKey='" + linkedIssueKey + '\'' +
'}';
}
And the problem is that I get error 415 Unsupported Media Type when sending data using Knockout.js:
ko.utils.postJson("/smth", ko.toJSON(data));
Where data is defined in the following way: ko.observableArray([]);
And populated with Task objects:
function Task(data){
this.linkName = ko.observable(data.linkName);
this.linkedIssueKey = ko.observable(data.linkedIssueKey).extend({
required: true
});
}
And by looking into Form Data in a Header information I see that Knockout.js sends data in a very weird way (part of Form Data has been omitted, because it's too long ):
Question is how can I solve this problem and why does this happen?
PS Back-end REST service works correctly. Tested with REST request sender by sending following request :
[{"linkName":"yo","linkedIssueKey":"zopa"},
{"linkName":"yo","linkedIssueKey":"zopa"}]
The problem is that ko.utils.postJSON
expects an array,
It's using the string as an indexer. (like "Hello"[0] is "H")
Try
ko.utils.postJson("/smth", [ko.toJSON(data)]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.