I am trying to create a dashboard in Grafana through an AJAX post method, hoping to create a dashboard through my website post, but I dont know why I always got console.log(“Client POST Error”);.( fixed - see my comment)
I am using Node.js as my web server.
But I encounter another problem is “Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”
Should I reconstruct from
website -> grafana
to
website -> webserver(reverse proxyserver) -> grafana?
Is that will be a better way?
This is my JavaScript code:
var dashboard = {
"dashboard": {
"id": null,
"title": "WebPos",
"tags": [ "templated" ],
"timezone": "browser",
"rows": [
{
}
],
"schemaVersion": 6,
"version": 0
},
"overwrite": false
};
$(document).ready(function(){
$('#post').click(function(){
console.log("Client Clicked");
$.ajax({
url: 'http://10.3.1.25:3000/api/dashboard/db',
type:'post',
dataType:'json',
data:JSON.stringify(dashboard),
success: function(data){
console.log("Client POST Success");
},
error: function(e){
console.log("Client POST Error");
console.log(e);
},
beforeSend: setHeader
});
});
});
function setHeader(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer eyJrIjoiVGJyWTJuS2dyNVV0VG93Z3FqR0IIQ1hYblJVUlpBTEQiLCJuIjoiZWRpIiwiaWQiOjF9');
xhr.setRequestHeader('Access-Control-Allow-Headers:*');
}