I am trying to post a .json file as a single document to an ArangoDB collection, from within javascript/ajax.
I can post (to ArangoDB) the .json file using curl, so that works I can post (to ArangoDB) simple {key: value} pairs using AJAX, so that works, but combining the two seems to be a bridge too far. I have spent a couple of nights trying to get this far, so any help would be hugely appreciated. thanks in advance.
My javascript code looks like this
var database_URL = prompt("Please enter your URL", "http://xxx..xxx.xxxx.:8529/_db/collection_name/_api/document?collection=PA_Users&createCollection=false");
var fd = new FormData();
var selectedFile = document.getElementById('files').files[0];
console.log(selectedFile.name);// this works
fd.append(selectedFile.name,selectedFile);
var settings = {
url : database_URL,
type : "POST",
headers: {
'Authorization': "Basic " + btoa(username1 + ":" + passwrd1)
},
data: fd,
processData: false,
success: function(data) {
// display feedback to user
alert("booyah");
},
error: function(data) {
// display feedback to user
alert("boo hoo");
}
};
$.ajax(settings);
I think you should use /_api/import instead of /_api/document:
HTTP Interface for Bulk Imports
Here is a small working example (without authorization):
$.ajax({
type: "POST",
url:
'/_api/import?type=auto&collection=' +
encodeURIComponent(yourCollectionID) +
'&createCollection=false',
data: file,
processData: false,
contentType: 'json',
dataType: 'json',
complete: function(xhr) {
if (xhr.readyState === 4 && xhr.status === 201) {
callback(false);
} else {
try {
var data = JSON.parse(xhr.responseText);
if (data.errors > 0) {
// error
}
else {
// success
}
}
catch (err) {
console.log(err);
}
}
}
});
}
The api supports a few input formats:
1.) Single document
{name: "Jonny"}
2.) Multiple documents (one doc in each row)
{name: "Jonny"}
{name: "Adam"}
{name: "Peter"}
3.) Multiple documents in JSON array
[{name: "Jonny"}, {name: "Adam"}, {name: "Peter"}]
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.