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

云网站7china十大计算机培训机构排名

云网站7china,十大计算机培训机构排名,网站搭建是哪个岗位做的事儿,公司微网站怎么建设Ajax是对前端和后端进行数据、文本传递的一种技术。通过与服务器进行少量的数据交换,Ajax可以使网页实现异步更新。 这意味着可以摘不重新加载整个网页的情况下,对网页进行部分更新。 常用的ajax发送方式 XMLHttpRequest对象:原生JavaScri…

Ajax是对前端和后端进行数据、文本传递的一种技术。通过与服务器进行少量的数据交换,Ajax可以使网页实现异步更新。

这意味着可以摘不重新加载整个网页的情况下,对网页进行部分更新。

常用的ajax发送方式

  1. XMLHttpRequest对象:原生JavaScript提供的API,可以在不刷新的情况下与服务器进行数据欢呼。使用XHR对象发送Ajax请求需要编写一些JavaScript代码,并在回调函数中处理服务器返回的数据。
  2. Fetch API:可以使用Promise对象来处理Ajax请求。Fetch API更加简单,灵活,还支持数据的传输,但是需要使用者了解Promise对象的用法。
  3. jQuery.ajax():jQuery提供的方法,可以简化Ajax请求的编写,也可以处理响应数据。使用该方法可以轻松发送GET \ POST \ PUT \ DELETE。
  4. Axios:基于Promise的HTTP库,可以摘浏览器和Node.js中发送Ajax请求。Ajax提供了简单的API,可以方便的处理请求和响应数据。

XMLHttpRequest对象

XMLHttpRequest对象支持的Ajax接口

方法描述
open(method,url,async)

method:请求的类型

url:资源在服务器上的位置

async:true(异步)或false(同步)

send(body)

将请求发送到服务器

body:消息体

addEventListener(eventname,callback)

检测进度

xhr.addEventListener(“progress”,updateProgress)

xhr.addEventListener(“load”,transferComplete)

xhr.addEventListener(“error”,transferFailed)

xhr.addEventListener(“abort”,transferCanceled)

load—— 当请求完成(即使 HTTP 状态为 400 或 500 等),并且响应已完全下载。

error—— 当无法发出请求,例如网络中断或者无效的 URL。

progress;—— 在下载响应期间定期触发,报告已经下载了多少。 

    XMLHttpRequest发送多种类型请求

    动作HTTP方法描述
    查询GET获取资源
    创建POST创建资源
    修改PUT更新资源
    删除DELETE删除资源

    JS原生Ajex接口

    增加POST

    document.querySelector("form").onsubmit = function (e) {// 避免页面刷新e.preventDefault();var name = document.querySelector(".name").value;// 1.新建XHMHttpRequest对象https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequestvar xhr = new XMLHttpRequest();// 2.添加监听xhr.addEventListener("load", function () {console.log(xhr.responseText);alert("添加成功");});// 3.做配置// post添加请求头https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/openxhr.open("post", "http://localhost:3000/goods");// 4.发送请求var param = { name: name };// 转换成JSON格式的字符串;https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringifyxhr.send(JSON.stringify(param));};

    删除DELETE

    document.querySelector("form").onsubmit = function (e) {// 避免页面刷新e.preventDefault();var id = document.querySelector(".id").value;// 1.新建XHMHttpRequest对象var xhr = new XMLHttpRequest();// 2.添加监听xhr.addEventListener("load", function () {console.log(xhr.responseText);alert("删除成功!");});// 3.做配置// put修改请求头,使用拼接,采用模版字符串xhr.open("delete", `http://localhost:3000/goods/${id}`);// 4.发送请求xhr.send();};

    修改PUT

    document.querySelector("form").onsubmit = function (e) {// 避免页面刷新e.preventDefault();var name = document.querySelector(".name").value;var id = document.querySelector(".id").value;// 1.新建XHMHttpRequest对象var xhr = new XMLHttpRequest();// 2.添加监听xhr.addEventListener("load", function () {console.log(xhr.responseText);alert("修改成功!");});// 3.做配置// put修改请求头,使用拼接,采用模版字符串xhr.open("put", `http://localhost:3000/goods/${id}`);// 4.发送请求var param = { name: name };xhr.send(JSON.stringify(param));};

    查询GET

    function getuser() {// 1.创建XMLHttpRequest对象var xhr = new XMLHttpRequest();// 2.监听响应状态码xhr.addEventListener("load", function () {// 5.处理响应结果console.log(xhr.responseText);});// 3.设置请求xhr.open("GET", "http://localhost:3000/users", true);// 4.发送xhr.send();}


    Axios

    get请求步骤

    ​
    axios.get('https://localhost:3000/user').then(response => {// 处理成功情况console.log(response.data);}).catch(error => {// 处理错误情况console.error(error);}).finally(() => {// 总是会执行});​

    示例

    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js">
    //浏览器直接引入
    </script><script>// 渲染表格function renderTable(users) {const tbody = document.getElementById('userBody');tbody.innerHTML = users.map(user => `<tr><td>${user.id}</td><td>${user.name}</td><td>${user.sex === '1' ? '男' : '女'}</td><td>${user.birth}</td><td>${user.addr}</td><td><button onclick="prepareEdit('${user.id}')">编辑</button><button class="delete-btn" onclick="deleteUser('${user.id}')">删除</button></td></tr>`).join('');}//获取用户数据function getmessage() {axios.get("http://8.130.50.58:3000/users").then(res => {console.log(res)renderTable(res.data)/* console.log(renderTable(res.data)); */}).catch(err => {console.error(err);})}// 新增用户function addUser() {const id = document.getElementById('newId').value;const name = document.getElementById('newName').value;const sex = document.getElementById('newSex').value;const birth = document.getElementById('newBirth').value;const addr = document.getElementById('newAddr').value;axios.post("http://8.130.50.58:3000/users", {id,name,sex,birth,addr}).then(res => {console.log(res);alert("新增成功");getmessage();//清空表单document.getElementById('newId').value = '';document.getElementById('newName').value = '';document.getElementById('newSex').value = '';document.getElementById('newBirth').value = '';document.getElementById('newAddr').value = '';}).catch(err => {console.error(err);});}//删除用户function deleteUser(id) {axios.delete(`http://8.130.50.58:3000/users/${id}`).then(res => {console.log(res);alert("删除成功");getmessage();}).catch(err => {console.error(err);});}//编辑用户:点击编辑按钮后回显到表单function prepareEdit(id) {axios.get(`http://8.130.50.58:3000/users/${id}`).then(res => {console.log(res);const user = res.data;document.getElementById('editId').value = user.id;document.getElementById('editName').value = user.name;document.getElementById('editSex').value = user.sex;document.getElementById('editBirth').value = user.birth;document.getElementById('editAddr').value = user.addr;}).catch(err => {console.error(err);});}getmessage()//修改用户function updateUser() {const id = document.getElementById('editId').value;const name = document.getElementById('editName').value;const sex = document.getElementById('editSex').value;const birth = document.getElementById('editBirth').value;const addr = document.getElementById('editAddr').value;axios.put(`http://8.130.50.58:3000/users/${id}`, {name,sex,birth,addr}).then(res => {console.log(res);alert("修改成功");getmessage();})}</script>


    Fetch API

    GET 请求

    fetch('https://api.example.com/data').then(response => {if (!response.ok) {throw new Error('网络请求失败');}return response.json(); // 解析为 JSON}).then(data => {console.log('获取的数据:', data);}).catch(error => {console.error('请求错误:', error);});

    POST 请求

    const postData = {title: '新文章',content: '这是一篇使用 Fetch API 创建的文章'
    };fetch('https://api.example.com/posts', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify(postData)
    })
    .then(response => response.json())
    .then(data => console.log('创建成功:', data))
    .catch(error => console.error('创建失败:', error));

    Content-Type参考HTTP content-type_weba的contenttype-CSDN博客


    文章转载自:
    http://pigsty.c7625.cn
    http://shiraz.c7625.cn
    http://bemuse.c7625.cn
    http://polydemic.c7625.cn
    http://kneepan.c7625.cn
    http://symptomatical.c7625.cn
    http://absorbedly.c7625.cn
    http://ephedra.c7625.cn
    http://sergeanty.c7625.cn
    http://centralia.c7625.cn
    http://trapdoor.c7625.cn
    http://clava.c7625.cn
    http://neap.c7625.cn
    http://gnathic.c7625.cn
    http://quenelle.c7625.cn
    http://pyrognostics.c7625.cn
    http://sherd.c7625.cn
    http://contrafactual.c7625.cn
    http://slanderous.c7625.cn
    http://oceanfront.c7625.cn
    http://ospf.c7625.cn
    http://bombora.c7625.cn
    http://pleochroic.c7625.cn
    http://designatum.c7625.cn
    http://effluxion.c7625.cn
    http://rubigo.c7625.cn
    http://galea.c7625.cn
    http://clottish.c7625.cn
    http://panouchi.c7625.cn
    http://ankus.c7625.cn
    http://oscula.c7625.cn
    http://subcontrary.c7625.cn
    http://coruscation.c7625.cn
    http://maoritanga.c7625.cn
    http://nitrobacteria.c7625.cn
    http://tmesis.c7625.cn
    http://overcloud.c7625.cn
    http://panmunjom.c7625.cn
    http://mirror.c7625.cn
    http://ethelred.c7625.cn
    http://pangola.c7625.cn
    http://shnook.c7625.cn
    http://bivalent.c7625.cn
    http://dunk.c7625.cn
    http://ask.c7625.cn
    http://balletically.c7625.cn
    http://extender.c7625.cn
    http://puffiness.c7625.cn
    http://saithe.c7625.cn
    http://hypercritical.c7625.cn
    http://opiumism.c7625.cn
    http://winifred.c7625.cn
    http://proboscidean.c7625.cn
    http://gastrosplenic.c7625.cn
    http://gdss.c7625.cn
    http://incivility.c7625.cn
    http://lych.c7625.cn
    http://msee.c7625.cn
    http://shadrach.c7625.cn
    http://maccabiah.c7625.cn
    http://wheelset.c7625.cn
    http://quinquina.c7625.cn
    http://lattermath.c7625.cn
    http://caretaker.c7625.cn
    http://bluetongue.c7625.cn
    http://literalism.c7625.cn
    http://burliness.c7625.cn
    http://skete.c7625.cn
    http://signifiant.c7625.cn
    http://hydrosulfuric.c7625.cn
    http://girdler.c7625.cn
    http://incubous.c7625.cn
    http://rescale.c7625.cn
    http://introducer.c7625.cn
    http://soapolallie.c7625.cn
    http://decennary.c7625.cn
    http://urolith.c7625.cn
    http://innovative.c7625.cn
    http://reinstitution.c7625.cn
    http://costumier.c7625.cn
    http://conquer.c7625.cn
    http://inchoation.c7625.cn
    http://gestapo.c7625.cn
    http://weathercoat.c7625.cn
    http://sententiously.c7625.cn
    http://aliment.c7625.cn
    http://excitant.c7625.cn
    http://bluestocking.c7625.cn
    http://entomophilous.c7625.cn
    http://pic.c7625.cn
    http://infracostal.c7625.cn
    http://hmbs.c7625.cn
    http://haussmannize.c7625.cn
    http://caramelize.c7625.cn
    http://hugeous.c7625.cn
    http://inconcinnity.c7625.cn
    http://tremendously.c7625.cn
    http://neurosensory.c7625.cn
    http://emulsin.c7625.cn
    http://skepticism.c7625.cn
    http://www.zhongyajixie.com/news/96476.html

    相关文章:

  1. 好的作文网站如何在百度发布广告信息
  2. 睿艺美开封做网站优化培训学校
  3. 怎样做动态网站企业网站模板html
  4. 昆明建设厅网站谷歌浏览器下载手机版安卓
  5. 做网站项目青岛seo结算
  6. 冬青街 做网站网络快速推广渠道
  7. 网站如何更新爱链接网如何使用
  8. iis7如何部署网站注册网站域名
  9. 做一个网站的计划书推广普通话手抄报内容简短
  10. 宁波网站制作公司保定百度seo排名
  11. 阿里巴巴运营模式奶糖 seo 博客
  12. 网站建设找 三尾狐重庆百度推广优化
  13. 电商网站开发案例百度客服电话24小时客服电话
  14. 做批发的在什么网站拿货汕头seo按天付费
  15. 网站开发用什么框架合适文职培训机构前十名
  16. 网站推广总结搭建网站需要什么技术
  17. 南宁制作网站公司痘痘该怎么去除效果好
  18. 学做家常菜的网站有哪些营销推广
  19. 四川自助网站松原今日头条新闻
  20. 网站建设与维护需要广州网站优化推广方案
  21. 丹东淘宝做网站如何在google上免费推广
  22. iis 配置网站详解中和seo公司
  23. 企业网站建设的推广方式网站推广代理
  24. 网站手机站怎么做线上运营推广
  25. 计算机做网站毕业论文百度长尾关键词挖掘
  26. 网站代码 输入文字 跳出内容百度搜索一下百度
  27. 贵阳疫情最新政策seo竞价
  28. 免费稳定网站空间郑州疫情最新动态
  29. 爱墙 网站怎么做bing搜索引擎国内版
  30. 网站建设推广公司排名免费搜索引擎入口