中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

自媒體網(wǎng)站開發(fā)充電寶關(guān)鍵詞優(yōu)化

自媒體網(wǎng)站開發(fā),充電寶關(guān)鍵詞優(yōu)化,設(shè)計(jì)師做兼職的網(wǎng)站,團(tuán)購網(wǎng)站APP怎么做概述 前后端網(wǎng)絡(luò)請求工具 原生ajaxfetch apiaxios GET和POST請求 get只能發(fā)純文本 post可以發(fā)不同類型的數(shù)據(jù),要設(shè)置請求頭,需要告訴服務(wù)器一些額外信息 測試服務(wù)器地址 有一些公共的測試 API 可供學(xué)習(xí)和測試用途。這些 API 允許你發(fā)送 HTTP 請求…

概述

前后端網(wǎng)絡(luò)請求工具

  • 原生ajax
  • fetch api
  • axios

GET和POST請求

get只能發(fā)純文本

post可以發(fā)不同類型的數(shù)據(jù),要設(shè)置請求頭,需要告訴服務(wù)器一些額外信息

測試服務(wù)器地址

有一些公共的測試 API 可供學(xué)習(xí)和測試用途。這些 API 允許你發(fā)送 HTTP 請求(GET、POST 等),并從服務(wù)器獲取響應(yīng)。以下是一些常用的公共測試 API:

  1. JSONPlaceholder:
    • Base URL: https://jsonplaceholder.typicode.com
    • Example Endpoints:
      • Posts: /posts
      • Comments: /comments
      • Users: /users
    • Usage Example (GET): https://jsonplaceholder.typicode.com/posts/1
  2. ReqRes:
    • Base URL: https://reqres.in
    • Example Endpoints:
      • Users: /api/users
      • Single User: /api/users/2
      • Create User: /api/users
    • Usage Example (POST): https://reqres.in/api/users

原生ajax

前端頁面代碼

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<script src="ajax_get.js"></script>
</body>
</html>

GET

//原生ajax
const xhr = new XMLHttpRequest();
//xhr.open('GET', 'http://wuyou.com/common/get');
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts/1')
xhr.send();
xhr.onreadystatechange = function(){if(xhr.readyState == XMLHttpRequest.DONE && xhr.status === 200){console.log(JSON.parse(xhr.responseText));}
}

返回結(jié)果

在這里插入圖片描述

POST

const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://jsonplaceholder.typicode.com/posts');
xhr.setRequestHeader('Content-Type', 'application/json'); // 修改 Content-Type
xhr.send(JSON.stringify({title: 'foo',body: 'bar',userId: 1
}));xhr.onreadystatechange = function() {if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 201) {console.log(xhr.responseText); // 不解析 JSON,直接輸出響應(yīng)文本}
};

返回結(jié)果

請?zhí)砑訄D片描述

Axios

前端頁面代碼

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<script src="axios.min.js"></script>
<script src = 'axiosjs.js'></script>
</body>
</html>

直接傳輸

最簡單的axios使用方式,get函數(shù)中填寫請求的url

//用axios來get一個(gè)請求
axios.get('https://jsonplaceholder.typicode.com/posts/1').then(response => {console.log('GET Response:', response.data);}).catch(error => {console.error('GET Error:', error);});

返回結(jié)果

在這里插入圖片描述

異步傳輸

用異步的方式傳輸,在axios中配置地址,請求/響應(yīng)攔截器

//異步請求處理
//異步發(fā)送get請求
(async () => {try {const ins = axios.create({baseURL: 'https://jsonplaceholder.typicode.com',});// 請求攔截器ins.interceptors.request.use((config) => {console.log('發(fā)送了請求');return config;});// 響應(yīng)攔截器ins.interceptors.response.use((response) => {// 在這里可以對響應(yīng)數(shù)據(jù)進(jìn)行處理return response.data;},(error) => {// 在這里處理響應(yīng)錯(cuò)誤return Promise.reject(error);});const res = await ins.get('/posts/1');const res2 = await ins.post('/posts', {title: 'foo',body: 'bar',userId: 1,});console.log('GET 的結(jié)果:', res);console.log('POST 的結(jié)果:', res2);} catch (error) {console.error('發(fā)生錯(cuò)誤:', error);}
})();

返回結(jié)果

在這里插入圖片描述

Fetch

前端頁面代碼

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<script src = 'fetch_get.js'></script>
</body>
</html>

GET請求

fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(res => {if(res.ok){return res.json()}
})
.then(data =>{console.log(data)}).catch(error =>{console.error(error)
})

結(jié)果:
在這里插入圖片描述

POST請求

在參數(shù)處需要傳入一些配置項(xiàng)

//post在參數(shù)的地方需要傳入一些配置項(xiàng)const postData = {title: 'foo',body: 'bar',userId: 1
};
console.log("test")
fetch('https://jsonplaceholder.typicode.com/posts', {method: 'POST',headers:{'Content-Type': 'application/json'},body:JSON.stringify({postData})}
).then(res =>{if(res.ok){return res.json()}
})
.then(data =>{console.log(data)}
)

結(jié)果

在這里插入圖片描述

http://www.risenshineclean.com/news/56943.html

相關(guān)文章:

  • web仿網(wǎng)站開發(fā)視頻如何建立企業(yè)網(wǎng)站
  • 羅湖做網(wǎng)站的公司哪家好營銷方式和手段有哪些
  • 白城學(xué)做網(wǎng)站外貿(mào)平臺推廣
  • 長春網(wǎng)站建設(shè)推薦網(wǎng)誠傳媒網(wǎng)絡(luò)營銷的推廣手段
  • 如果做網(wǎng)站賺錢優(yōu)化關(guān)鍵詞的步驟
  • html怎么寫廣州網(wǎng)站優(yōu)化服務(wù)商
  • 怎么去跟客戶談網(wǎng)站建設(shè)四川seo優(yōu)化
  • 網(wǎng)站建設(shè)題庫核心關(guān)鍵詞舉例
  • 怎么建設(shè)h5網(wǎng)站怎樣做關(guān)鍵詞排名優(yōu)化
  • 怎么推廣產(chǎn)品最有效短視頻關(guān)鍵詞seo優(yōu)化
  • wordpress無法連接遠(yuǎn)程mysqlseo推廣人員
  • 真正能賺錢的網(wǎng)站app推廣平臺放單平臺
  • 工信部企業(yè)網(wǎng)站認(rèn)證搜索引擎有哪些平臺
  • 怎么做企業(yè)網(wǎng)站一級懸浮菜單一個(gè)萬能的營銷方案
  • 河南做網(wǎng)站聯(lián)系電話輿情網(wǎng)站直接打開的軟件
  • 廣東手機(jī)網(wǎng)站建設(shè)價(jià)格百度優(yōu)化
  • 做網(wǎng)站是什么工作天津網(wǎng)絡(luò)關(guān)鍵詞排名
  • 北京住房建設(shè)部網(wǎng)站南寧網(wǎng)站推廣大全
  • 新疆做網(wǎng)站哪家公司好廣東培訓(xùn)seo
  • 平臺網(wǎng)站兼職做sap關(guān)鍵詞優(yōu)化推廣排名多少錢
  • 投票活動網(wǎng)站怎么做seo學(xué)徒招聘
  • 廣告牌的樣式大全福清市百度seo
  • 天津營銷網(wǎng)站建設(shè)公司哪家好虎撲體育網(wǎng)體育
  • 499元做網(wǎng)站微信營銷技巧
  • 珠海市網(wǎng)站建設(shè)開發(fā)公司海南百度推廣公司
  • 網(wǎng)站建設(shè)的欄目百度推廣一個(gè)月費(fèi)用
  • 畢業(yè)設(shè)計(jì)網(wǎng)站做啥長沙建設(shè)網(wǎng)站制作
  • 如何建立一個(gè)網(wǎng)站放視頻適合小學(xué)生的新聞事件
  • 做二手網(wǎng)站有哪些2021百度熱搜年度榜
  • 佛山高端網(wǎng)站建設(shè)比較火的推廣軟件