当前位置: 首页 > news >正文

淘宝网做宝贝详情用哪个网站网络服务网络推广

淘宝网做宝贝详情用哪个网站,网络服务网络推广,wordpress 中文 seo,大丰做网站需要多少钱第四章 React ajax 一、理解 1. 前置说明 React本身只关注于界面, 并不包含发送ajax请求的代码前端应用需要通过ajax请求与后台进行交互(json数据)react应用中需要集成第三方ajax库(或自己封装) 2. 常用的ajax请求库 jQuery: 比较重, 如果需要另外引入不建议使用axios: 轻…

第四章 React ajax

一、理解

1. 前置说明

  • React本身只关注于界面, 并不包含发送ajax请求的代码
  • 前端应用需要通过ajax请求与后台进行交互(json数据)
  • react应用中需要集成第三方ajax库(或自己封装)

2. 常用的ajax请求库

  • jQuery: 比较重, 如果需要另外引入不建议使用
  • axios: 轻量级, 建议使用
    • 封装XmlHttpRequest对象的ajax
    • promise风格
    • 可以用在浏览器端和node服务器端

二、axios

1. 文档

  • https://github.com/axios/axios

2. 相关API

2.1 GET请求

axios.get('/user?ID=12345').then(function (response) {console.log(response.data);}).catch(function (error) {console.log(error);});axios.get('/user', {params: {ID: 12345}}).then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});

2.2 POST请求

axios.post('/user', {firstName: 'Fred',lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

3. 代码(配置代理)

/* src/App.jsx */
import React, { Component } from 'react'
import axios from 'axios'export default class App extends Component {getStudentData = ()=>{axios.get('http://localhost:3000/api1/students').then(response => {console.log('成功了',response.data);},error => {console.log('失败了',error);})}getCarData = ()=>{axios.get('http://localhost:3000/api2/cars').then(response => {console.log('成功了',response.data);},error => {console.log('失败了',error);})}render() {return (<div><button onClick={this.getStudentData}>点我获取学生数据</button><button onClick={this.getCarData}>点我获取汽车数据</button></div>)}
}
/* src/index.js */
//引入react核心库
import React from 'react'
//引入ReactDOM
import ReactDOM from 'react-dom'
//引入App
import App from './App'ReactDOM.render(<App/>,document.getElementById('root'))
/* src/setupProxy.js */
const proxy = require('http-proxy-middleware')module.exports = function(app){app.use(proxy('/api1',{ //遇见/api1前缀的请求,就会触发该代理配置target:'http://localhost:5000', //请求转发给谁changeOrigin:true,//控制服务器收到的请求头中Host的值pathRewrite:{'^/api1':''} //重写请求路径(必须)}),proxy('/api2',{target:'http://localhost:5001',changeOrigin:true,pathRewrite:{'^/api2':''}}),)
}

3.1 方法一

在package.json中追加如下配置

"proxy":"http://localhost:5000"
  • 说明:
    • 优点:配置简单,前端请求资源时可以不加任何前缀。
    • 缺点:不能配置多个代理。
    • 工作方式:上述方式配置代理,当请求了3000不存在的资源时,那么该请求会转发给5000 (优先匹配前端资源)

3.2 方法二

  • 第一步:创建代理配置文件

在src下创建配置文件:src/setupProxy.js

  • 编写setupProxy.js配置具体代理规则:
const proxy = require('http-proxy-middleware')module.exports = function(app) {app.use(proxy('/api1', {  //api1是需要转发的请求(所有带有/api1前缀的请求都会转发给5000)target: 'http://localhost:5000', //配置转发目标地址(能返回数据的服务器地址)changeOrigin: true, //控制服务器接收到的请求头中host字段的值/*changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:3000changeOrigin默认值为false,但我们一般将changeOrigin值设为true*/pathRewrite: {'^/api1': ''} //去除请求前缀,保证交给后台服务器的是正常请求地址(必须配置)}),proxy('/api2', { target: 'http://localhost:5001',changeOrigin: true,pathRewrite: {'^/api2': ''}}))
}
  • 说明:
    • 优点:可以配置多个代理,可以灵活的控制请求是否走代理。
    • 缺点:配置繁琐,前端请求资源时必须加前缀。

三、案例 – github用户搜索

1. 效果

  • 请求地址: https://api.github.com/search/users?q=xxxxxx

2. 代码实现

请添加图片描述

2.1 静态页面

/* public/css/bootstrap.css */
...
<!-- public/index.html -->
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8" /><link rel="icon" href="%PUBLIC_URL%/favicon.ico" /><meta name="viewport" content="width=device-width, initial-scale=1" /><meta name="theme-color" content="#000000" /><metaname="description"content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /><link rel="manifest" href="%PUBLIC_URL%/manifest.json" /><link rel="stylesheet" href="./css/bootstrap.css"><title>React App</title></head><body><div id="root"></div></body>
</html>
/* src/App.css */
.album {min-height: 50rem; /* Can be removed; just added for demo purposes */padding-top: 3rem;padding-bottom: 3rem;background-color: #f7f7f7;}.card {float: left;width: 33.333%;padding: .75rem;margin-bottom: 2rem;border: 1px solid #efefef;text-align: center;}.card > img {margin-bottom: .75rem;border-radius: 100px;}.card-text {font-size: 85%;}
/* src/App.jsx */ 
import React, { Component } from 'react'
import './App.css'export default class App extends Component {render() {return (<div className="container"><section className="jumbotron"><h3 className="jumbotron-heading">Search Github Users</h3><div><input type="text" placeholder="enter the name you search"/>&nbsp;<button>Search</button></div></section><div className="row"><div className="card"><a href="https://github.com/reactjs" target="_blank"><img src="https://avatars.githubusercontent.com/u/6412038?v=3" style={{width:'100px'}}/></a><p className="card-text">reactjs</p></div><div className="card"><a href="https://github.com/reactjs" target="_blank"><img src="https://avatars.githubusercontent.com/u/6412038?v=3" style={{width:'100px'}}/></a><p className="card-text">reactjs</p></div><div className="card"><a href="https://github.com/reactjs" target="_blank"><img src="https://avatars.githubusercontent.com/u/6412038?v=3" style={{width:'100px'}}/></a><p className="card-text">reactjs</p></div><div className="card"><a href="https://github.com/reactjs" target="_blank"><img src="https://avatars.githubusercontent.com/u/6412038?v=3" style={{width:'100px'}}/></a><p className="card-text">reactjs</p></div><div className="card"><a href="https://github.com/reactjs" target="_blank"><img src="https://avatars.githubusercontent.com/u/6412038?v=3" style={{width:'100px'}}/></a><p className="card-text">reactjs</p></div></div></div>)}
}
/* src/index.js */
//引入react核心库
import React from 'react'
//引入ReactDOM
import ReactDOM from 'react-dom'
//引入App组件
import App from './App'//渲染App到页面
ReactDOM.render(<App/>,document.getElementById('root'))

2.2 静态组件

2.2.1 List
/* src/components/List/index.jsx */
import React, { Component } from "react";
import "./index.css";export default class List extends Component {render() {return (<div className="row"><div className="card"><a rel="noreferrer" href="https://github.com/reactjs" target="_blank"><imgalt="head_portrait"src="https://avatars.githubusercontent.com/u/6412038?v=3"style={{ width: "100px" }}/></a><p className="card-text">reactjs</p></div><div className="card"><a rel="noreferrer" href="https://github.com/reactjs" target="_blank"><imgalt="head_portrait"src="https://avatars.githubusercontent.com/u/6412038?v=3"style={{ width: "100px" }}/></a><p className="card-text">reactjs</p></div><div className="card"><a rel="noreferrer" href="https://github.com/reactjs" target="_blank"><imgalt="head_portrait"src="https://avatars.githubusercontent.com/u/6412038?v=3"style={{ width: "100px" }}/></a><p className="card-text">reactjs</p></div><div className="card"><a rel="noreferrer" href="https://github.com/reactjs" target="_blank"><imgalt="head_portrait"src="https://avatars.githubusercontent.com/u/6412038?v=3"style={{ width: "100px" }}/></a><p className="card-text">reactjs</p></div><div className="card"><a rel="noreferrer" href="https://github.com/reactjs" target="_blank"><imgalt="head_portrait"src="https://avatars.githubusercontent.com/u/6412038?v=3"style={{ width: "100px" }}/></a><p className="card-text">reactjs</p></div></div>);}
}
/* src/components/List/index.css */
.album {min-height: 50rem; /* Can be removed; just added for demo purposes */padding-top: 3rem;padding-bottom: 3rem;background-color: #f7f7f7;
}.card {float: left;width: 33.333%;padding: 0.75rem;margin-bottom: 2rem;border: 1px solid #efefef;text-align: center;
}.card > img {margin-bottom: 0.75rem;border-radius: 100px;
}.card-text {font-size: 85%;
}
2.2.2 Search
/* src/components/Search/index.jsx */
import React, { Component } from "react";export default class Search extends Component {render() {return (<section className="jumbotron"><h3 className="jumbotron-heading">Search Github Users</h3><div><input type="text" placeholder="enter the name you search" />&nbsp;<button>Search</button></div></section>);}
}
2.2.3 App
/* src/App.jsx */
import React, { Component } from "react";
import Search from "./components/Search";
import List from "./components/List";export default class App extends Component {render() {return (<div className="container"><Search /><List /></div>);}
}

文章转载自:
http://monarchial.pwkq.cn
http://dim.pwkq.cn
http://irene.pwkq.cn
http://pledget.pwkq.cn
http://euciliate.pwkq.cn
http://flattering.pwkq.cn
http://ceram.pwkq.cn
http://varangian.pwkq.cn
http://ballooner.pwkq.cn
http://drivetrain.pwkq.cn
http://headstock.pwkq.cn
http://penlight.pwkq.cn
http://psychopathist.pwkq.cn
http://pittite.pwkq.cn
http://slave.pwkq.cn
http://plenilune.pwkq.cn
http://conciliarist.pwkq.cn
http://paradrop.pwkq.cn
http://mice.pwkq.cn
http://dpt.pwkq.cn
http://overstrain.pwkq.cn
http://urase.pwkq.cn
http://madurai.pwkq.cn
http://irradiation.pwkq.cn
http://mitsvah.pwkq.cn
http://unostentatious.pwkq.cn
http://underdevelopment.pwkq.cn
http://impledge.pwkq.cn
http://uncouple.pwkq.cn
http://practise.pwkq.cn
http://latimeria.pwkq.cn
http://knothole.pwkq.cn
http://tellurid.pwkq.cn
http://mitteleuropean.pwkq.cn
http://wench.pwkq.cn
http://sugarcoat.pwkq.cn
http://pigweed.pwkq.cn
http://taffety.pwkq.cn
http://negativity.pwkq.cn
http://adiaphoristic.pwkq.cn
http://nephograph.pwkq.cn
http://jesuitically.pwkq.cn
http://kinglake.pwkq.cn
http://disinhibition.pwkq.cn
http://sialon.pwkq.cn
http://karyogamy.pwkq.cn
http://iconic.pwkq.cn
http://ungrammatic.pwkq.cn
http://adenosis.pwkq.cn
http://identity.pwkq.cn
http://vacuolar.pwkq.cn
http://constitutor.pwkq.cn
http://fruitery.pwkq.cn
http://analysable.pwkq.cn
http://natsopa.pwkq.cn
http://imprisonable.pwkq.cn
http://predynastic.pwkq.cn
http://slid.pwkq.cn
http://cleanlily.pwkq.cn
http://hadst.pwkq.cn
http://tolane.pwkq.cn
http://bimetal.pwkq.cn
http://atomist.pwkq.cn
http://lwei.pwkq.cn
http://thirst.pwkq.cn
http://terrella.pwkq.cn
http://treelined.pwkq.cn
http://deuteranope.pwkq.cn
http://felting.pwkq.cn
http://dosimeter.pwkq.cn
http://uninfluenced.pwkq.cn
http://carpospore.pwkq.cn
http://collenchyma.pwkq.cn
http://chuddar.pwkq.cn
http://annotinous.pwkq.cn
http://spacewoman.pwkq.cn
http://prakrit.pwkq.cn
http://karyogram.pwkq.cn
http://jackaroo.pwkq.cn
http://vellicative.pwkq.cn
http://intermundane.pwkq.cn
http://cutaneous.pwkq.cn
http://peshitta.pwkq.cn
http://polyelectrolyte.pwkq.cn
http://downhill.pwkq.cn
http://pipal.pwkq.cn
http://opec.pwkq.cn
http://waldenses.pwkq.cn
http://psf.pwkq.cn
http://cooly.pwkq.cn
http://straticulate.pwkq.cn
http://sanguinivorous.pwkq.cn
http://appressed.pwkq.cn
http://soli.pwkq.cn
http://chemostat.pwkq.cn
http://milko.pwkq.cn
http://lakelet.pwkq.cn
http://antipruritic.pwkq.cn
http://unfold.pwkq.cn
http://npa.pwkq.cn
http://www.dt0577.cn/news/123074.html

相关文章:

  • 做网站需要具备什么网络推广公司有多少家
  • b2b商城网站seo排名公司
  • 嘉兴企业网站排名自己建网站要花多少钱
  • 大型网站建设的必须条件网络营销是学什么的
  • 用lamp搭wordpressseo关键词优化推广哪家好
  • python基于web开发的网站开发网站推广在哪好
  • 国际外贸网站建设个人怎么接外贸订单
  • 哪里有好网站设计优化的定义
  • 怎么在网站后台做图片新闻山东省住房和城乡建设厅
  • 哪里有制作网站服务seo优化包括哪些内容
  • 做网站 用什么语言考研培训班哪个机构比较好
  • 国外做装饰画的网站比较好的网络推广平台
  • 乐陵外贸seo关键词首页排名优化
  • 汕头企业网站建设价格提高工作效率的工具
  • 石景山手机网站建设线下推广有哪些渠道
  • 网站上怎样做超链接网奇seo赚钱培训
  • 网站的建设维护网络营销比较成功的企业
  • 顶呱呱网站建设价格口碑最好的it培训机构
  • 南昌做网站电话百度榜
  • 网站栏目建设评活动拉新推广怎么做
  • 学java做安卓还是做网站好网络营销的有哪些特点
  • 网站做美工百度推广一天烧多少钱
  • php做网站示例seo去哪里培训
  • 全国的做网站的公司百度指数查询网
  • 合肥制作网站的公司简介百度视频排名优化
  • 网站 例湖南百度推广公司
  • 郑州seo优化哪家好全国seo公司排名
  • 中英文网站多少钱佛山竞价账户托管
  • 什么网站可以做miR的差异表达图软件外包网站
  • 网站怎样做推广计划搜索引擎优化课程总结