Javascript fetch data from php

I am trying to fetch values from php file

api.php

      

my javascript page has code like this.[this code is not on page api.php]

            fetch['//localhost/react_task/react-webpack-boilerplate/php/api.php', {
        method: 'get',
        // may be some code of fetching comes here
    }].then[function[response] {
            if [response.status >= 200 && response.status < 300] {
                return response.text[]
            }
            throw new Error[response.statusText]
        }]
        .then[function[response] {
            console.log[response];
        }]

can you please guide me how to fetch value of variable from php file by using fetch.

asked Sep 9, 2016 at 14:33

Manpreet OberoiManpreet Oberoi

3473 gold badges4 silver badges11 bronze badges

3

You are not echoing the value, so it won't be send.


answered Sep 9, 2016 at 14:36

7

Your php needs to output the value you want. A simple way to do this is to use echo. For example:

echo 'come!! fetch this value';

answered Sep 9, 2016 at 14:40

RickCoxDevRickCoxDev

1651 silver badge9 bronze badges

1

You should use echo to display it in php then it will be available in you JavaScript reponse.

answered Aug 26, 2019 at 1:35

TL;DR How can I use fetch[] to return data from a PHP file?

Hi all. I am having difficulty using fetch[] to return data from PHP. Specifically, I have a PHP script jout.php that defines a simple array:

Chủ Đề