Spring mvc 加 ajax 跨域 问题解决方案

        页面端:

           

   	function getMsg(){
	    $.ajax({
	        type: 'POST',
	        url: "http://demo.sq.com/user/getCardMessage",
	        data: {id: 23},
	        dataType:  'JSONP',
	        jsonp:"callback",
	        jsonpCallback:"success_jsonpCallback",
	        timeout: 10000,
	        error: function () {
	            alert("失败!");
	        },
	        success: function (data) {
	           $("#name").html(data.name);
	        }
	    });
	}

    Controller端:

 @ResponseBody
    @RequestMapping(value = "getCardMessage", produces = "application/x-javascript;charset=UTF-8")
    public String getCardMessage(Long id) {
           return "success_jsonpCallback" + "({\"name\":\"小苹果\"})"; }

猜你喜欢

转载自limeng650419.iteye.com/blog/2200043