[英]How to Post a .json file to ArangoDB using Ajax
I am trying to post a .json file as a single document to an ArangoDB collection, from within javascript/ajax.我正在尝试从 javascript/ajax 中将 .json 文件作为单个文档发布到 ArangoDB 集合。
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.我可以使用 curl 发布(到 ArangoDB).json 文件,这样我就可以使用 AJAX 发布(到 ArangoDB)简单的 {key: value} 对,这样就可以了,但是将两者结合起来似乎是一个桥梁。 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我的 javascript 代码如下所示
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:我认为您应该使用 /_api/import 而不是 /_api/document:
HTTP Interface for Bulk Imports批量导入的 HTTP 接口
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: api 支持几种输入格式:
1.) Single document 1.) 单个文件
{name: "Jonny"}
2.) Multiple documents (one doc in each row) 2.) 多个文档(每行一个文档)
{name: "Jonny"}
{name: "Adam"}
{name: "Peter"}
3.) Multiple documents in JSON array 3.) JSON 数组中的多个文档
[{name: "Jonny"}, {name: "Adam"}, {name: "Peter"}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.