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;
    }

}



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页