jsonp+springmvc 跨域访问

html:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

<script type="text/javascript" src="js/jquery.js" ></script>
<script type="text/javascript">
$.ajax({
type:"get",
url:"http://10.14.36.20:8080/abc",
dataType: 'jsonp',
jsonp:"callback",
jsonpCallback:"callbackFun",
success: function (data) {
        alert("加载成功:"+data);
    }
});

</script>

</body>

</html>


java:

package com.example.demo.controller;

import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


    @RequestMapping("/abc")
    public Object abc(){
        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue("hello, abc");
        mappingJacksonValue.setJsonpFunction("callbackFun");
        return mappingJacksonValue;
    }

}



猜你喜欢

转载自blog.csdn.net/fisher_yu01/article/details/80320021
今日推荐