# fetch

{% embed url="https://learn.javascript.ru/fetch" %}

```python
# ------------------------------------------------------------------------------
def send_response(code, **data):
	"""
	"""
	response = {}  # словник-відповідь в JS
	response['code'] = code
	response['data'] = data

	log.debug(f'Send Response = {response}')
	print(json.dumps(response))
	sys.exit(code)
#def

max_num = _db.query('list', 'SELECT MAX(act) FROM receipt')
if _db.error:
	send_response( 301, **{
		'mess'  : tr('ERR_DB'),
		'debug' : _db.errmsg if int(_conf.get('debug')) else ''
	})
else:
	max_num = max_num[0][0]  # query('list',...) повертає [(N,)]
	log.debug(max_num)
	if max_num is None:
		# коли ще нема записів - NULL
		next_num = 1;
	else:
		next_num = int(max_num) + 1
		# next_num = int(max_num) + 1  # query з 'list' повертає [(N,)]

	log.debug(f'SQL-запит 401 ОК')
	send_response( 0, **{
		'mess' : 'ok',
		'num'  : next_num,
	})


```

```javascript
const ERR_RUNTIME          = '{{ ERR_RUNTIME }}';
const SRV_REFUSED_REQUEST  = '{{ SRV_REFUSED_REQUEST }}'
const SRV_UNKNOWN_RESPONSE = '{{ SRV_UNKNOWN_RESPONSE }}';

/* --------------------------------------------------------------------- */
function get_next_number() {

    function _success(responce) {
        if (responce && 'code' in responce && 'data' in responce)
        {
            if (responce.code == 0)
            {
                if ('num' in responce.data)
                    nNumb.value = responce.data.num;
            }
            else
            {
                alert('danger', responce.data.mess);
                if (responce.data.debug)
                    console.log(responce.data.debug);
            }
        }
        else
            alert('danger', SRV_UNKNOWN_RESPONSE);

        buttons_disabled(true);
    }

    fetch("/equip/receiptgetnum/", { method: "POST"})
    .then(resp => {
        if (resp.status != 200) {
            alert('danger', SRV_REFUSED_REQUEST +` Status: ${resp.status}`);
        }
        return resp.json();
    })
    .then(resp => _success(resp))
    .catch(err => {
            console.dir(err)
            alert('danger', ERR_RUNTIME)
        });
}

```
