Tell me how to implement a REST API request by the type of this: https://dadata.ru/api/suggest/ . I would like to implement a request for React. Made requests for axios. It is difficult to understand what parameters and how to transfer using the REST API in this example.
2 answers
Something like this
axios.post( 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/fio', // url { 'query': FIO }, // data { headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Token ' + API_KEY }, // `withCredentials` indicates whether or not cross-site Access-Control requests // should be made using credentials withCredentials: true // Может быть надо, а может быть нет } // config ) .then(function (response) { console.log(response); // Здесь обработать ответ как надо }) .catch(function (error) { console.log(error); }); From React should get FIO (query text)API_KEY - key issued during registration
- oneThe right and working solution. Works correctly without withCredentials - Alexander Agapov
- The only thing I cannot get is response and use its value as a new state in react. - Alexander Agapov
response.data.suggestions[0]...- Sergey- The value in the console is displayed .. But outside of the promise, this value can not be used. - Alexander Agapov
|
Using jQuery you can do this:
function () { var _this = this; $.ajax({ type: "POST", url: "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/fio", data: {"query": "Викт"}, contentType: "application/json", headers: { "Authorization": "Token 1234" // тут должен быть ваш токен }, success: function (data) { _this.setState(data); } }); } This function must be added to the appropriate handlers for your component. And do not forget to configure CORS, since you are accessing another domain. I think that under axios it is not difficult to remake.
|