mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-01 04:56:45 +00:00
AJAX function to update a model endpoint
- Grabs the CSRF token cookie (required!) - If final mode, adds '_is_final' parameter to request
This commit is contained in:
parent
41e031d4b4
commit
11e7a34aa2
@ -1,7 +1,26 @@
|
|||||||
|
var jQuery = window.$;
|
||||||
|
|
||||||
|
// using jQuery
|
||||||
|
function getCookie(name) {
|
||||||
|
var cookieValue = null;
|
||||||
|
if (document.cookie && document.cookie != '') {
|
||||||
|
var cookies = document.cookie.split(';');
|
||||||
|
for (var i = 0; i < cookies.length; i++) {
|
||||||
|
var cookie = jQuery.trim(cookies[i]);
|
||||||
|
// Does this cookie string begin with the name we want?
|
||||||
|
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
||||||
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cookieValue;
|
||||||
|
}
|
||||||
|
|
||||||
function inventreeGet(url, filters={}) {
|
function inventreeGet(url, filters={}) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: url,
|
url: url,
|
||||||
type: 'get',
|
type: 'GET',
|
||||||
data: filters,
|
data: filters,
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
@ -16,6 +35,36 @@ function inventreeGet(url, filters={}) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function inventreeUpdate(url, data, final=false) {
|
||||||
|
if (final) {
|
||||||
|
data["_is_final"] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Middleware token required for data update
|
||||||
|
//var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
|
||||||
|
var csrftoken = getCookie('csrftoken');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
beforeSend: function(xhr, settings) {
|
||||||
|
xhr.setRequestHeader('X-CSRFToken', csrftoken);
|
||||||
|
},
|
||||||
|
url: url,
|
||||||
|
type: 'put',
|
||||||
|
data: data,
|
||||||
|
dataType: 'json',
|
||||||
|
success: function(response, status) {
|
||||||
|
response['_status_code'] = status;
|
||||||
|
console.log('UPDATE object to ' + url + ' - result = ' + status);
|
||||||
|
return response;
|
||||||
|
},
|
||||||
|
error: function(xhr, ajaxOptions, thrownError) {
|
||||||
|
console.error('Error on UPDATE to ' + url);
|
||||||
|
console.error(thrownError);
|
||||||
|
return {error: thrownError};
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Return list of parts with optional filters
|
// Return list of parts with optional filters
|
||||||
function getParts(filters={}) {
|
function getParts(filters={}) {
|
||||||
return inventreeGet('/api/part/', filters);
|
return inventreeGet('/api/part/', filters);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user