Unable to connect my React app to WordPress API? What am I doing wrong?

[ad_1]

I have hosted a wordpress site on my localhost on a certain port number. Lets call that port number xxxx. When I open my browser and type

http://localhost:xxxx

I am able to reach the main home page of my wordpress site and I can see my posts over there.

So clearly its working.

Now in my react app, I am using axios.get to connect to that page and attempt to get the posts in JSON format.

So I’m typing in:

axios.get(‘http://localhost:xxxx/index.php/wp-json’).then(res=>{console.log(res)}).catch(err=>{console.log(err)})

I am getting a ” AxisError Network Error “.

But the interesting thing is, when I try another local file path URL, I instead get a ” 404 not found error “.

What could be causing this? Why is my react app not able to connect to localhost:xxxx? Is there something wrong with my url?

[ad_2]
2 Comments
  1. I don’t think [http://localhost](http://localhost):xxxx/index.php/wp-json is valid.

    WP urls are [http://localhost](http://localhost):xxxx/wp-json – i.e. no index.php. You can remove the index.php by creating an htaccess file, which you can generate by going to Settings > Permalinks.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer