Ajax总结

1.第一个简单的ajax程序

Ajax和 JSON

简介

Ajax 即 Asynchronous Javascript And XML(异步 JavaScript 和 XML),是指一种创建交互式、快速动态网页应用的网页开发技术,无需重新加载整个网页的情况下,能够更新部分网页的技术。

JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

Ajax不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的web应用程序的技术。

C/S

增强B/S的体验性

B/S:未来的主流,并且会爆发式的持续增长;

产品链:H5+网页+客户端+手机端(安卓和ios)+小程序

1.首先创建一个html页面

<html>
 <head>
   <title>$ajax$</title>
  <!--<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>-->
   </script>
 </head>
 <script src="../statics/js/jquery-3.4.1.js"></script>
 <body>
       <div id="app" οnclick="getAjax()">点击获得数据 懂?</div>
	        <div id="app" οnclick="getAjax2()">点击获得数据2</div>
	   
	   
 </body>
 <script type="text/javascript">
	  
	 function getAjax(){
		 var name = "name";
		 $.ajax({
		  		  type:"GET",
		  		  data:name,
		  		  url:`http://localhost:8086/ajax/a1/${name}`,
		  		  success:function(res){
		  			  debugger;
		  			   console.log(res);
					    window.alert(res);
			
		  		  }
		  		  
		  		
		 }) ;
	 }
	 
	 function getAjax2(){
	 		 let name = "name";
	 		 $.ajax({
				     
	 		  		  type:"GET",
					  url: "http://localhost:8086/ajax/a2?name="+name,
	 		  		   
	 		  		  
	 		  		  success:function(res){
	 		  			  debugger;
	 		  			   console.log(res);
	 					    window.alert(res);
	 			
	 		  		  }
	 		  		  
	 		  		
	 		 }) ;
	 }
 	 
 	 
 	 
 </script>
</html>
在这里插入代码片

2.编辑controller

package com.liang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Controller
@RequestMapping("/ajax")
@CrossOrigin
public class AjaxController {

    //第一种方式,服务器要返回一个字符串,直接使用response
    @GetMapping("/a1/{name}")
    @ResponseBody
    public String ajax1( @PathVariable  String name) throws IOException {
//        if ("admin".equals(name)){
//            response.getWriter().print("true");
//        }else{
//            response.getWriter().print("false");
//        }
        return  "ok  lwh";
    }

    //第一种方式,服务器要返回一个字符串,直接使用response
    @GetMapping("/a2")
    @ResponseBody
    public String ajax2(   String name) throws IOException {
//        if ("admin".equals(name)){
//            response.getWriter().print("true");
//        }else{
//            response.getWriter().print("false");
//        }
        return  "ok";
    }

}

在这里插入图片描述
3.伪造ajax小案例额

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>伪造ajax</title>
</head>
<body>

<script type="text/javascript">
    window.onload = function(){
        var myDate = new Date();
        document.getElementById('currentTime').innerText = myDate.getTime();
    };

    function LoadPage(){
        var targetUrl =  document.getElementById('url').value;
        console.log(targetUrl);
        document.getElementById("iframePosition").src = targetUrl;
    }

</script>

<div>
    <p>请输入要加载的地址:<span id="currentTime"></span></p>
    <p>
        <input id="url" type="text" value="https://www.baidu.com/"/>
        <input type="button" value="提交" onclick="LoadPage()">
    </p>
</div>

<div>
    <h3>加载页面位置:</h3>
    <iframe id="iframePosition" style="width: 100%;height: 500px;"></iframe>
</div>

</body>
</html>

4 .最简单的api

package com.liang.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @RequestMapping("aaa")
    public String aaa(){
        return "获取成功";
    }
}



<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>伪造ajax</title>
</head>
<body>

<script type="text/javascript">
    var vue = new Vue({
        el:"#app",
        data:{
            msg:""
        },
        methods:function () {
         let url = "aaa";
         axios.get(url).
         then(function (res) {
             this.vue.msg = res.data;
         })
        }
    })

</script>

<div id="app">
    <p>请输入要加载的地址:<span id="currentTime"></span></p>
    <p>
        <input id="url" type="text" value="https://www.baidu.com/"/>
        <input type="button" value="提交" onclick="LoadPage()">
    </p>
</div>

<div>
    <h3>加载页面位置:</h3>
    <iframe id="iframePosition" style="width: 100%;height: 500px;"></iframe>
</div>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java中的战斗机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值