Resolving react projects in cross-domain and Axios encapsulation using

Source: Internet
Author: User

The latest days to learn a little react, found a few problems, estimated new into the pit of the students will also encounter, the following I first listed what time

1. Request Cross-domain issues

2. How to initiate a request

3. Simple package of Axios

Global installation of Create-react-app scaffolding to help us create a new react project

NPM install-g create-react-app

Then create the REACT project

Create-react-app XXX Project Name

Then install dependencies, and run the React project

Installing NPM Install

Run NPM start

First we solve the cross-domain problem

Open the Package.json file generated by the project and modify the file contents as follows

If you're too lazy to write like this,

Complete wording

This solves the cross-domain problem.

Let's talk about it. If you use Axios in react

Install Axios First

NPM Install Axios--save-dev

Then create a new server.js file in our src directory and write down the following code

Then we can use our Axios on the react component.

Write a react component, and then introduce our newly encapsulated server.js, write a login function, then execute the function after the component has finished rendering

Open a browser to view the results returned

Seeing the above results shows that we have successfully solved the cross-domain problem and got the data returned in the background. is not very simple, hurry up and try it yourself.

Resolving react projects in cross-domain and Axios encapsulation using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.