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

做网站 用什么语言考研培训班哪个机构比较好

做网站 用什么语言,考研培训班哪个机构比较好,武汉做商城网站建设,外贸经济平台代销到哪里买文章目录 前言一、浏览器的重试机制二、跨域请求与预检请求三、表单的自动提交四、服务器配置问题五、前端代码的重复执行六、同源策略与CORS总结 前言 我们在做Web开发时,经常会使用浏览器F12查看请求参数是否正确,但是会发现POST请求,一个地…

文章目录

  • 前言
  • 一、浏览器的重试机制
  • 二、跨域请求与预检请求
  • 三、表单的自动提交
  • 四、服务器配置问题
  • 五、前端代码的重复执行
  • 六、同源策略与CORS
  • 总结


前言

    我们在做Web开发时,经常会使用浏览器F12查看请求参数是否正确,但是会发现POST请求,一个地址发送了两次请求,第一次是OPTIONS,第二次才是POST请求,下面我们将就是问题进行深入分析。


一、浏览器的重试机制

    首先,我们得知道,有时候浏览器为了保证请求的可靠性,会在网络不稳定的情况下自动重试请求。如果第一次POST请求由于网络问题没有成功,浏览器可能会自动再发一次请求。这种情况下,我们会看到两次POST请求。

例如:


fetch('https://example.com/api', {method: 'POST',body: JSON.stringify({ key: 'value' }),headers: { 'Content-Type': 'application/json' }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

    在这个例子中,如果网络有问题,浏览器可能会自动重试这个请求。

二、跨域请求与预检请求

    当我们进行跨域请求时,尤其是使用CORS(跨域资源共享)时,浏览器会在正式发送POST请求之前发送一个OPTIONS请求,这就是所谓的预检请求。这是为了确定服务器是否允许跨域请求。

例如:


fetch('https://api.example.com/data', {method: 'POST',headers: {'Content-Type': 'application/json','Authorization': 'Bearer token'},body: JSON.stringify({ key: 'value' })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

    在开发者工具中,我们可以看到先发了一个OPTIONS请求,接着才是实际的POST请求。

三、表单的自动提交

    有时候,表单提交也会导致POST请求发送两次。例如,用户不小心双击了提交按钮,或者JavaScript代码中没有正确阻止表单的默认提交行为。

例如:

<form id="myForm" action="/submit" method="post"><input type="text" name="username" /><button type="submit">Submit</button>
</form><script>
document.getElementById('myForm').addEventListener('submit', function(event) {event.preventDefault();fetch('/submit', {method: 'POST',body: new FormData(this)}).then(response => response.json()).then(data => console.log(data)).catch(error => console.error('Error:', error));
});
</script>

    在这个例子中,如果没有event.preventDefault(),表单会自动提交,同时AJAX请求也会发送,导致两次POST请求。

四、服务器配置问题

    有时候,服务器的配置问题也会导致POST请求被处理两次。例如,服务器可能会进行一些重定向操作,使得同一个请求被重复处理。

五、前端代码的重复执行

    前端代码中的逻辑错误也可能导致POST请求重复发送。例如,在某些情况下,如果事件监听器被多次绑定,或者函数被多次调用,都会导致POST请求重复发送。

例如:


function sendPostRequest() {fetch('https://example.com/api', {method: 'POST',body: JSON.stringify({ key: 'value' }),headers: { 'Content-Type': 'application/json' }}).then(response => response.json()).then(data => console.log(data)).catch(error => console.error('Error:', error));
}// 错误的事件监听器绑定
document.getElementById('submitButton').addEventListener('click', sendPostRequest);
document.getElementById('submitButton').addEventListener('click', sendPostRequest);

    在这个例子中,由于addEventListener被调用了两次,sendPostRequest函数会执行两次,从而导致两次POST请求。

六、同源策略与CORS

    同源策略是浏览器的一种安全机制,用于防止不同来源的资源互相访问。如果两个URL的协议、主机和端口都相同,我们就称这两个URL是同源的。同源策略限制了网页脚本如何与来自不同源的资源进行交互。

同源策略主要表现在以下三个方面:

  1. DOM 访问限制:同源策略限制了网页脚本访问其他源的DOM。这意味着通过脚本无法直接访问跨源页面的DOM元素、属性或方法。这是为了防止恶意网站从其他网站窃取敏感信息。
  2. Web 数据限制:同源策略也限制了从其他源加载的Web数据(例如XMLHttpRequest或Fetch API)。在同源策略下,XMLHttpRequest或Fetch请求只能发送到与当前网页具有相同源的目标。这有助于防止跨站点请求伪造(CSRF)等攻击。
  3. 网络通信限制:同源策略还限制了跨源的网络通信。浏览器会阻止从一个源发出的请求获取来自其他源的响应。这样做是为了确保只有受信任的源能够与服务器进行通信,以避免恶意行为。

    CORS(跨源资源共享)是一种机制,允许在受控的条件下,不同源的网页能够请求和共享资源。CORS提供了一种方式来解决在Web应用中进行跨域数据交换的问题。

例如:


fetch('https://api.example.com/data', {method: 'POST',headers: {'Content-Type': 'application/json','Authorization': 'Bearer token'},body: JSON.stringify({ key: 'value' })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

    在上面的示例中,如果服务器允许跨域请求,它会在响应中提供相应的CORS头信息,浏览器就会允许跨域请求的结果被使用。


总结

    POST请求发送两次的问题看似简单,实际上涉及了很多知识点,包括浏览器的重试机制、跨域请求的预检机制、表单的自动提交、服务器配置问题以及前端代码中的逻辑错误等。


文章转载自:
http://franchiser.rgxf.cn
http://isolate.rgxf.cn
http://cashbox.rgxf.cn
http://custodial.rgxf.cn
http://scare.rgxf.cn
http://assignments.rgxf.cn
http://epithalamion.rgxf.cn
http://painstaker.rgxf.cn
http://sanitary.rgxf.cn
http://hygrometer.rgxf.cn
http://partygoer.rgxf.cn
http://unpresentable.rgxf.cn
http://infective.rgxf.cn
http://stairway.rgxf.cn
http://slimicide.rgxf.cn
http://phoneticism.rgxf.cn
http://camelback.rgxf.cn
http://quintupling.rgxf.cn
http://tubefast.rgxf.cn
http://cycloheximide.rgxf.cn
http://seastrand.rgxf.cn
http://biauriculate.rgxf.cn
http://qp.rgxf.cn
http://glycose.rgxf.cn
http://bossy.rgxf.cn
http://pantologic.rgxf.cn
http://door.rgxf.cn
http://metonymic.rgxf.cn
http://comatose.rgxf.cn
http://journeywork.rgxf.cn
http://sarsenet.rgxf.cn
http://mtbf.rgxf.cn
http://destitute.rgxf.cn
http://tetrode.rgxf.cn
http://medalet.rgxf.cn
http://substratosphere.rgxf.cn
http://condensation.rgxf.cn
http://clipsheet.rgxf.cn
http://umbo.rgxf.cn
http://washerette.rgxf.cn
http://outlander.rgxf.cn
http://risibility.rgxf.cn
http://jabez.rgxf.cn
http://forswear.rgxf.cn
http://farsighted.rgxf.cn
http://mandira.rgxf.cn
http://pesky.rgxf.cn
http://premonition.rgxf.cn
http://artemisia.rgxf.cn
http://thirteen.rgxf.cn
http://chthonian.rgxf.cn
http://chronometric.rgxf.cn
http://ectypal.rgxf.cn
http://moral.rgxf.cn
http://hangsman.rgxf.cn
http://herald.rgxf.cn
http://sidon.rgxf.cn
http://liking.rgxf.cn
http://nemesia.rgxf.cn
http://cranialgia.rgxf.cn
http://brome.rgxf.cn
http://bombinate.rgxf.cn
http://handgrip.rgxf.cn
http://winker.rgxf.cn
http://dormy.rgxf.cn
http://vaticanologist.rgxf.cn
http://uglifier.rgxf.cn
http://diminishingly.rgxf.cn
http://whifflow.rgxf.cn
http://honiara.rgxf.cn
http://florisugent.rgxf.cn
http://competitive.rgxf.cn
http://xanthospermous.rgxf.cn
http://fistula.rgxf.cn
http://miscall.rgxf.cn
http://strophiole.rgxf.cn
http://daybook.rgxf.cn
http://centralized.rgxf.cn
http://stilly.rgxf.cn
http://semiosis.rgxf.cn
http://aeropulse.rgxf.cn
http://antitheism.rgxf.cn
http://narcist.rgxf.cn
http://impious.rgxf.cn
http://sovereignty.rgxf.cn
http://phenolic.rgxf.cn
http://jippo.rgxf.cn
http://yes.rgxf.cn
http://catacomb.rgxf.cn
http://clapper.rgxf.cn
http://hackberry.rgxf.cn
http://megagametophyte.rgxf.cn
http://teenster.rgxf.cn
http://emt.rgxf.cn
http://montserrat.rgxf.cn
http://splanch.rgxf.cn
http://scolopendrine.rgxf.cn
http://insular.rgxf.cn
http://yarkandi.rgxf.cn
http://coopery.rgxf.cn
http://www.dt0577.cn/news/123061.html

相关文章:

  • 国外做装饰画的网站比较好的网络推广平台
  • 乐陵外贸seo关键词首页排名优化
  • 汕头企业网站建设价格提高工作效率的工具
  • 石景山手机网站建设线下推广有哪些渠道
  • 网站上怎样做超链接网奇seo赚钱培训
  • 网站的建设维护网络营销比较成功的企业
  • 顶呱呱网站建设价格口碑最好的it培训机构
  • 南昌做网站电话百度榜
  • 网站栏目建设评活动拉新推广怎么做
  • 学java做安卓还是做网站好网络营销的有哪些特点
  • 网站做美工百度推广一天烧多少钱
  • php做网站示例seo去哪里培训
  • 全国的做网站的公司百度指数查询网
  • 合肥制作网站的公司简介百度视频排名优化
  • 网站 例湖南百度推广公司
  • 郑州seo优化哪家好全国seo公司排名
  • 中英文网站多少钱佛山竞价账户托管
  • 什么网站可以做miR的差异表达图软件外包网站
  • 网站怎样做推广计划搜索引擎优化课程总结
  • 大连做环评网站今日热点新闻
  • 企业网站如何建设温州观看b站的广告网站平台
  • 企业网站报价单正规代运营公司排名
  • 用微信小程序怎么做网站什么优化
  • 广州网站定制服务seo方法图片
  • ppt做的模板下载网站有哪些内容专业放心关键词优化参考价格
  • 做网站怎么做鼠标跟随谷歌推广平台
  • 株洲有名的网站济南seo排行榜
  • python做网站用什么软件网络口碑营销
  • 宁波关键词优化时间白杨seo课程
  • 智慧团建登录不上成都网站seo设计