а

Laravel, ajax, api token, gantt.load("/api/data")


#1

I need send ajax request with token, gantt.load("/api/data"), read this https://docs.dhtmlx.com/gantt/desktop__server_side.html,

tried this

gantt.ajax.get({
url: “/api”,
headers: {
“Authorization”: “Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b”
}
}).then(function (xhr) {
gantt.parse(xhr.responseText)
});

Help plz, maybe i’m doing something wrong


#2

Also tried send token from this method, but it gave error {“status”:“Authorization Token not found”}

view.blade.php

gantt.config.xml_date = “%Y-%m-%d %H:%i:%s”;
gantt.init(“gantt_here”);
gantt.load("/api/data");
var dp = gantt.createDataProcessor({
url: “/api/data”,
mode:“REST”,
headers: {
“Content-Type”: “application/x-www-form-urlencoded”,
“Authorization”: "Bearer " +token
}
});
dp.init(gantt);
dp.setTransactionMode(“REST”);


#3

Solution is

var dp = gantt.createDataProcessor(function (entity, action, data, id) {
// const services = {
// “task”: this.taskService,
// “link”: this.linkService
// };
// const service = services[entity];
switch (action) {
case “create”:
return gantt.ajax.post({
url: “/api/” + entity,
headers: {
‘Authorization’: 'Bearer ’ + token,
‘Content-Type’: ‘application/json’
},
data: JSON.stringify(data)
});
break;

case “update”:
return gantt.ajax.put({
url: “/api/”+entity+"/" + id,
headers: {
‘Authorization’: 'Bearer ’ + token,
‘Content-Type’: ‘application/json’
},
data: JSON.stringify(data)
});
break;

case “delete”:
return gantt.ajax.del({
url: “/api/”+entity+"/"+ id,
headers: {
‘Authorization’: 'Bearer ’ + token,
‘Content-Type’: ‘application/json’
},
data: JSON.stringify(data)
});
// .then(function (response) {
// console.log(response)
// })
break;
}
});