#编者注
因为要编写一个统一的xmlrpc调用与rest,里面涉及到了跨域调用问题。随即有如下内容javascript
#Spring的跨域
##Spring 4.2.x 编者本来是想在Spring响应内容添加跨域请求,但因为很差划定跨域范围,产生了拆分想法。但通过查询Spring文档后发现,最新稳定版本提供了跨域支持**@CrossOrigin**,随即咱们就能够在Controller当中添加注解来完成。css
@Controller @RequestMapping(value = "/default/Api") public class XmlrpcServerController { @CrossOrigin @RequestMapping(method = RequestMethod.POST) public Object xmlrpc_api(@RequestBody String body) throws Exception { //get body System.out.println(body); //doing return result; }
#JSON信息的接收 除了咱们要接收上面需求的xml文件之外,rest接收还应当包括其余对象。经过搜索发现须要经过ajax把json以data的形式进行发送。html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 101 Template</title> <!-- Bootstrap --> <link href="http://localhost:8080/resources/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> </head> <body> <h1>你好,世界!</h1> <script type="text/javascript"> function myFunction() { var json = "{ \"username\":\"admin\", \"password\":\"yanfa\" }"; $.ajax({ type:"POST", url:"http://localhost:8080/tactic/ticket", dataType:"json", contentType:"application/json", data:json, success : function(data) { alert("新增成功!" + data); }, error : function(data) { alert(data) } }); } </script> <a class="btn btn-default" role="button" onclick="myFunction()">Link</a> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="http://localhost:8080/resources/bootstrap/3.3.5/js/bootstrap.min.js"></script> </body> </html>