M
M
Maxim Zolotoy2019-06-10 12:48:56
JavaScript
Maxim Zolotoy, 2019-06-10 12:48:56

How to send a JSON object to a foreign API using the GET method from JS?

You need to get data from the transport company. Their documentation says


To get the cost and delivery time, you need to send a json object to the CDEK server. When passing a json object, the appropriate Content-Type must be specified in the header.


api.cdek.ru/calculator/calculate_price_by_json.php
Content-Type: application/json
api.cdek.ru/calculator/calculate_price_by_jsonp.php
dataType : "jsonp" (from js)
api.cdek.ru/calculator/calculate_price_by_json_req...
Content-Type: application/x-www-form-urlencoded

How can this be done from JS? I asked tech support but they didn't help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-06-10
@rockon404

XMLHttp​Request​.set​Request​Header()
Using Fetch: Supplying Request Parameters

A
Anatoly Medvedev, 2019-06-10
@balamyt92

var xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);
xhr.onload = function () {
  // Запрос завершен. Здесь можно обрабатывать результат.
};
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send({ form: 'data' });

Well, something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question