前后端分离html ssm,前后端分离请求ssm返回数据
當你的開發(fā)是前后端分離時,前臺發(fā)起ajax請求,請求后臺數(shù)據(jù)時會出現(xiàn)跨域問題,為前后臺開發(fā)人員帶來很多不便,如果你用的是springMVC,那么這個問題在后臺非常好解決。
springMVC為我們提供了@CrossOrigin
只需要為想要進行跨域訪問的controller加入@CrossOrigin就能正常訪問并接收數(shù)據(jù)
代碼如下:
測試跨域var aa=function(){
$.ajax({
url:"http://localhost:8080/setschool/haha",
type:"get",
success:function(mess){
console.log(mess);
$.each(mess.extend.ccc,function(index,c){
$("#div1").append($("
});
}
});
}
package com.lzy.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lzy.util.Haha;
import com.lzy.util.Msg;
@Controller
public class TestCorssDomain {
@CrossOrigin
@ResponseBody
@RequestMapping("/haha")
public Msg haha() {
List list=new ArrayList<>();
Haha haha=null;
for(int i=0;i<10;i++) {
haha=new Haha();
haha.setName("Tom"+i);
haha.setAge(i+"");
list.add(haha);
}
System.out.println(list);
return Msg.success().add("ccc", list);
}
}
圖片為證:
如果沒有加@CrossOrigin標記控制臺就會報如下跨域請求失敗信息
Failed to load http://localhost:8080/setschool/bb: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的前后端分离html ssm,前后端分离请求ssm返回数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python安装错误 building
- 下一篇: 12c oracle 修改内存_还在等1