要访问服务器首先需要由客户端主动发出请求,在实际的操作中,我们可以通过多种方式向服务器发起请求。根据不同的场景需求,使用不同的请求方式可以达到不同的效果。
请求的方式
-
地址栏输入
-
超链接
-
Form表单
-
ajax
-
请求转发
-
重定向
案例实操
1 地址栏输入
在浏览器地址栏直接输入要访问的地址即可,此种方式可以看做是访问服务器的起始操作。
2 超链接
使用超链接也可以向服务器发出请求
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" contenteditable="false" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(255, 255, 255); position: relative !important; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><a href="http://www.baidu.com">百度</a></pre>
3 Form表单
当需要向服务器发送请求,并且传输一些用户输入的数据时,我们优先选择form表单的方式发起请求。

4 ajax
通过ajax发起的请求,属于异步请求,能实现局部刷新的效果,是一种比较常用的请求方式。
通过jQuery中的ajax(),get(),post(),getJSON()等方法都能发送请求
5 请求转发
通过服务器内部将请求进行一次转发,可以请求到其他资源(后面讲)
6 重定向
服务器通过给定一个新资源的地址,响应回客户端后,客户端自动再次发送一个请求到新资源的地址处。(后面讲)
扩展
Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式、快速动态网页应用的网页开发技术,无需重新加载整个网页的情况下,能够更新部分网页的技术。
通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。
网友评论