E
E
Eduard Hoffman2020-10-21 17:21:56
PHP
Eduard Hoffman, 2020-10-21 17:21:56

Vk mini app on react. How to generate json response on php side?

Greetings.
I decided to deal with the creation of applications on VK MiniApps and got stuck on the obvious.
Required by making a request to the php script from the application. Get data from it in json for further work with them.
We have a php script for the test:

<?php
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
$json = array(
          'site_url' => array(
              'URL' => 1234,
              ),
              );
header('Content-Type: application/json; charset=utf-8');
echo json_encode($json);
?>

It gives us a json response: {"site_url":{"URL":1234}}
On the application side, I use the following part of the code:
axios
      .get("https://site.pw/app/zx.php")
      .then((response) => {
        console.log(response.data);
      })
      .catch((error) => {
        console.log(error);
      });
  }

When accessing it, it gives an error.
Error: Network Error
at createError (https://oe8vd.csb.app/node_modules/axios/lib/core/createError.js:16:15)
at XMLHttpRequest.handleError (https://oe8vd.csb.app/node_modules/axios/lib/adapters/xhr.js:91:14)

At the same time, making a call to the link ( https://free.currencyconverterapi.com/api/v5/conve... )
Which produces the same json, everything is read. The error seems to be in the formation of the json itself in php.
Here is the codeendbox project https://codesandbox.io/s/alexastr-oe8vd?file=/src/...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eduard Hoffman, 2020-10-21
@Gens

The reason was found. But I did not understand how to solve it. The problem turned out to be due to cloudflare. They change the header or keep it in the cache. I cleared the cache, but it didn't help. Also tried Development mode. But it didn't help either. Everything works on the server without cloudflare proxying. In which direction to dig further I do not know.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question