0

Im trying to send a post request with axios to php. Im a bit lost, still new to this stuff. in my react.js I have this. Im trying to send the value of searchTerm

axios.post(`http://www.fake.com/script.php`,{
  searchTerm:'test',
})
.then(function(response){
  console.log(response)
})
.catch(function(error){
  console.log(error);
})

In the script.php file which I have hosted on my site, I have this:

if(!empty($_POST)) {
    $searchTerm = $_POST["searchTerm"];
    echo $searchTerm;
    echo '<script>console.log('.$searchTerm.')</script>';
}

nothing is echoed on the actual page.

On the console, I have 3 things.

  1. XHR finished loading: OPTIONS "http://www.fake.com/script.php"
  2. data object, which includes config,data,headers,etc.
  3. XHR finished loading: POST "http://www.fake.com/script.php"
12
  • So what happens? What do you see in your console? Commented May 28, 2018 at 4:27
  • I just edited the OP to show that Commented May 28, 2018 at 4:36
  • That's the network traffic logging. What do you see in your Console? You should expect either the result of console.log(response) or console.log(error). Perhaps try giving them some labels to help identification, eg console.log('response', response) and console.error(error) Commented May 28, 2018 at 4:38
  • I just did that, Im getting a response, which is the data object #2 on the op. But shouldnt it also console.log the actual search term as I specified in the script.php. Seems like maybe the php file isnt receiving the searchterm Commented May 28, 2018 at 4:46
  • No, the response from your PHP is just text / HTML. It will be in the response.data property. You haven't inserted it into your document and even then, it's not going to just execute the <script> blocks (see stackoverflow.com/q/1891947/283366) Commented May 28, 2018 at 4:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.