Ajax发送post请求以及跨域问题解决

https://www.w3school.com.cn/jquery/ajax_post.asp

post1

	function post(jd,wd) {
    
    
				$.post("http://localhost:8080//jwd/getp",
				    {
    
    
				      jd:jd,
				      wd:wd
				    },
				    function(result){
    
    
				      console.log(result.msg+result.code)
				    })
				}

post2 -同

function post2(jd,wd){
    
    
				$.post("http://localhost:8080//jwd/getp",
				{
    
    
					 jd:jd,
					 wd:wd
				},
				   function(data){
    
    
				     console.log("Data Loaded: " + data.msg+" "+data.code);
				   });
				}

跨域问题解决

加个注解 @CrossOrigin
参考文章
https://blog.csdn.net/qq_37978449/article/details/81456098

 @CrossOrigin
    @RequestMapping(path="/getp",method = {
    
    RequestMethod.POST})
    public  @ResponseBody
    Object getp(String jd,String wd){
    
    
        System.out.println("get");
        try {
    
    
            System.out.println(jd+" "+wd +" "+ new Date());
            return Result.init(200,"成功","");
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return Result.init(-200,"失败","");
        }
    }

全部代码

<!DOCTYPE html>
<html>
	<head> 
		<meta charset="utf-8"> 
		<title></title> 
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
	</head>
	<body>
		<p id="demo">点击按钮获取您当前坐标(可能需要比较长的时间获取):</p>
		<button onclick="getLocation()">点我</button>
		<script>
			var x = document.getElementById("demo");

			function getLocation() {
      
      
				if (navigator.geolocation) {
      
      
					navigator.geolocation.getCurrentPosition(showPosition);
				} else {
      
      
					x.innerHTML = "该浏览器不支持获取地理位置。";
				}
			}

			function showPosition(position) {
      
      
				x.innerHTML = "纬度: " + position.coords.latitude +
					"<br>经度: " + position.coords.longitude;
					//alert(position.coords.latitude+":"+position.coords.longitude);
					deleteBill(position.coords.latitude,position.coords.longitude);
			}
			
			function deleteBill(jd,wd) {
      
      
				var url = 'http://huawei.free.svipss.top//jwd/get?jd='+jd+'&wd='+wd;
				var fn = function(result) {
      
      
					//弹出提示
					console.log(result.msg)
				}
				$.get(url, fn, 'json')
			}
			function dtchecks() {
      
      
				var timer = setInterval(function() {
      
      
					getLocation()
					console.log(13)
					//deleteBill(2200,558989)
					post(2200,558989)
					//post2(2200,558989)
			
				}, 3000);
				}
				
				$(function() {
      
      
					dtchecks();
				});
			function post(jd,wd) {
      
      
				$.post("http://localhost:8080//jwd/getp",
				    {
      
      
				      jd:jd,
				      wd:wd
				    },
				    function(result){
      
      
				      console.log(result.msg+result.code)
				    })
				}
				function post2(jd,wd){
      
      
				$.post("http://localhost:8080//jwd/getp",
				{
      
      
					 jd:jd,
					 wd:wd ,
				},
				   function(data){
      
      
				     console.log("Data Loaded: " + data.msg+" "+data.code);
				   });
				}
		</script>
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_40711092/article/details/120111687