I
I
inavo2020-04-23 20:19:06
Vue.js
inavo, 2020-04-23 20:19:06

Why is nothing returned when sending via axios?

I send a simple request to a php file, but an empty request is returned.

axios.post('/ajax/data.php', {test: 'test'})
.then((response) => {
let data = response['data'];
   console.log(data);
})
.catch((error) => {
   console.log('error', error);
});

php file
echo 'post= ' . json_encode($_POST);

result post= []

File available, static string '123' will be returned.

I can't figure out what's the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
inavo, 2020-04-23
@inavo

In general, understood.
Rewrote it like this:

const bodyFormData = new FormData();
bodyFormData.set('data', JSON.stringify({
   url: this.url,
   test: 'test'
}));

axios({
  method: 'post',
  url: '/ajax/data.php',
  data: bodyFormData,
})

You can also do this:
axios.post(
  '/ajax/data.php',
  'data=' + JSON.stringify({
    url: this.url,
    test: 'test'
  })
)

php file:
$data = json_decode( html_entity_decode( stripslashes ($_POST['data']) ) );
echo json_encode($data);

W
WhiteJamer, 2020-04-23
@Whitejamer

I can assume that you need to do not echo but use return.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question