diff --git a/InvenTree/static/script/inventree/api.js b/InvenTree/static/script/inventree/api.js index 536430d4c8..ea7ddb00c9 100644 --- a/InvenTree/static/script/inventree/api.js +++ b/InvenTree/static/script/inventree/api.js @@ -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={}) { $.ajax({ url: url, - type: 'get', + type: 'GET', data: filters, dataType: 'json', 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 function getParts(filters={}) { return inventreeGet('/api/part/', filters);