日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

024_Fetch

發布時間:2025/5/22 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 024_Fetch 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. fetch更加簡單的數據獲取方式, 功能更加強大、更加靈活, 是基于Promise實現的。

2. fetch語法結構

fetch(url).then(fun1).then(fun2).......catch(fun)

3. 新建一個名為Promise的動態Web工程

3.1. 編寫FetchAjax.java

package com.bjbs.fa;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class FetchAjax extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String id = req.getParameter("id");resp.getWriter().write("Fetch Ajax get Request... id = " + id);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

3.2. 編寫FetchAjaxPost.java

package com.bjbs.fa;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class FetchAjaxPost extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String uname = req.getParameter("uname");String pwd = req.getParameter("pwd");resp.getWriter().write("Fetch Ajax post Request... uname = " + uname + ", pwd = " + pwd);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

3.3. 編寫FetchAjaxPostJson.java

package com.bjbs.fa;import java.io.BufferedReader; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class FetchAjaxPostJson extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {BufferedReader br = req.getReader();String result = "", rl = null;while((rl = br.readLine()) != null) {result += rl;}System.out.println("Fetch Ajax post Request Json Param... result = " + result);String res = "{\"code\":1,\"result\":\"success.\"}";resp.getWriter().write(res);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }

3.4. 修改web.xml

?3.5. 編寫FetchGet.html

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>Fetch基本用法-get</title></head><body><script type="text/javascript">fetch("http://localhost:8080/Fetch/fa.action?id=123").then(function(res){// text()方法屬于fetchAPI的一部分, 它返回一個Promise實例對象, 用于獲取后臺返回的數據return res.text();}).then(function(data){document.write(data + "<br />");});// get參數傳遞fetch("http://localhost:8080/Fetch/fa.action?id=456", {method: 'get'}).then(function(res){return res.text();}).then(function(data){document.write(data + "<br />");});</script></body> </html>

3.6. 編寫FetchPost.html

<!DOCTYPE html> <html><head><meta charset="utf-8" /><title>Fetch基本用法-post</title></head><body><script type="text/javascript">// post參數傳遞fetch("http://localhost:8080/Fetch/fap.action", {method: 'post',body: 'uname=lisi&pwd=123',headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(res){return res.text();}).then(function(data){document.write(data + "<br />");});// post傳遞json參數fetch("http://localhost:8080/Fetch/fapj.action", {method: 'post',body: JSON.stringify({uname: 'zhangsan', pwd: '456'}),headers: {'Content-Type': 'application/json'}}).then(function(res){return res.json();}).then(function(data){document.write("code = " + data.code + ", result = " + data.result + "<br />");});</script></body> </html>

3.7. 運行項目, 訪問FetchGet.html

3.8. 運行項目, 訪問FetchPost.html?

總結

以上是生活随笔為你收集整理的024_Fetch的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。