shimatta-kenkyusho/shimatta_kenkyusho/static/js/kenyusho-api-v1.js

54 lines
2.6 KiB
JavaScript

function api_ajax_request(method, url, onSuccess, onFail, sendData) {
var xmlhttp = new XMLHttpRequest();
// csrftoken is set globally
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
//console.log("Success:"+xmlhttp.responseText);
onSuccess(method, url, JSON.parse(xmlhttp.responseText));
} else {
onFail(method, url, xmlhttp.status, xmlhttp.responseText);
}
}
}
xmlhttp.open(method, url);
xmlhttp.setRequestHeader('X-CSRFToken', csrftoken);
if (sendData === null || typeof sendData === 'undefined') {
xmlhttp.send();
} else {
xmlhttp.send(JSON.stringify(sendData));
}
}
function api_ajax_request_without_send(method, url, onSuccess, onFail) {
return api_ajax_request(method, url, onSuccess, onFail, null);
}
function api_search_user(search, onSuccess, onFail) {
return api_ajax_request_without_send('GET', api_urls_v1['user-list']+`?search=${encodeURIComponent(search)}`, function(method, url, json) {onSuccess(json);}, onFail);
}
function api_search_component(search, onSuccess, onFail) {
return api_ajax_request_without_send('GET', api_urls_v1['component-list']+`?search=${encodeURIComponent(search)}`, function(method, url, json) {onSuccess(json);}, onFail);
}
function api_get_component_from_id(id, onSuccess, onFail) {
return api_ajax_request_without_send('GET', api_urls_v1['component-list']+`?search=${encodeURIComponent(id)}`, function(method, url, json) {onSuccess(json.results[0]);}, onFail);
}
function api_search_component_types(search, onSuccess, onFail) {
return api_ajax_request_without_send('GET', api_urls_v1['component-type-list']+`?search=${encodeURIComponent(search)}`, function(method, url, json) {onSuccess(json);}, onFail);
}
function api_search_package(search, onSuccess, onFail) {
return api_ajax_request_without_send('GET', api_urls_v1['package-list']+`?search=${encodeURIComponent(search)}`, function(method, url, json) {onSuccess(json);}, onFail);
}
function api_search_manufacturer(search, onSuccess, onFail) {
return api_ajax_request_without_send('GET', api_urls_v1['manufacturer-list']+`?search=${encodeURIComponent(search)}`, function(method, url, json) {onSuccess(json);}, onFail);
}
function api_search_distributor(search, onSuccess, onFail) {
return api_ajax_request_without_send('GET', api_urls_v1['distributor-list']+`?search=${encodeURIComponent(search)}`, function(method, url, json) {onSuccess(json);}, onFail);
}