springmvc,jquery跨域问题

jquery的ajax方法报错403,说是跨域问题,但是我全是本地localhost,只不过html页面是随意放置的,不是在项目中;但是只要一用ajax方法就会出问403的跨域问题,我直接用表单的action提交请求的时候都能正常访问到后台的方法,代码如下

<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<script>
    var param = {phone : '10086'};
        var paramJSON = JSON.stringify(param);
        console.info(paramJSON);
         $.ajax({  
                type: "post", // 请求方式  
                url: "http://localhost:8888/order/demo2", //url地址  
                data:paramJSON, //数据  
                contentType: "application/json",  
                dataType: "json",  
                success: function (response, ifo) {  
                    alert("success");  
                }, error: function () {  
                    alert("error");  
                }  
            }); 
</script>

// java 接口
@RequestMapping(value = "/demo2",method = RequestMethod.POST)
    public String getDemo2(@RequestBody Demo demo){
        System.out.println();
//        response.addHeader("Access-Control-Allow-Origin", "*");
        return "success";
    }
阅读 4.1k
2 个回答

url地址使用相对地址试下

html页面要放到项目中才行,或者使用node请求

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进