the front end adopts vue+webpack, listening port 8000
the back end adopts the combination of java (struts2+hibernate+spring), and tomcat listening port 8080
the front end wants to get the json data returned from the backend, but gets it as null? Initially judged as a back-end problem, find a solution
tested with jquery"s ajax method and also obtained as null
the front end displays the jsonp code of the data component:
jsonp: function(url, data={}, callbackName = "callback") {
return new Promise((resolve, reject) => {
console.log(url)
console.log(data)
console.log(callbackName)
window.__onGetData__ = data => resolve(data)
console.log(window.__onGetData__)
let queryString = Object.keys(data).map(k => `${k}=${data[k]}`).join(`&`)
console.log(queryString)
let script = document.createElement("script")
document.head.appendChild(script)
script.src = `${url}?${callbackName}=__onGetData__`
script.addEventListener("error", ()=>{ reject("load error")})
document.head.removeChild(script)
})
},
showData: function(){
this.jsonp("http://localhost:8080/QAsys/user_getAll")
.then(data =>{
console.log(data)
tableData = data;
}).catch(err => {
console.log(err)
})
}
returns the action code of json:
public class UserAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
private HttpServletRequest request; //request-api
private HttpServletResponse response;
private ObjectMapper mapper = new ObjectMapper();//jackson
private String result = new String();//
private Page page = new Page(); //
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response){ this.response = response;}
public void setResult(String result){
this.result = result;
}
@Autowired
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String getAllUser(){
//
page.setOffset(0);
page.setLength(10);
response.setContentType("text/plain");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Access-Control-Allow-Origin", "*");//
response.setDateHeader("Expires", 0);
try{
List<User> list = userService.getAllUser(page.getOffset(), page.getLength());
page.setMax(list.size());//
//json
result = mapper.writeValueAsString(list);
System.out.println(result);
PrintWriter out = response.getWriter();
String jsonpCallback = request.getParameter("callback");//
out.println(jsonpCallback+"("+result+")");//jsonp
}catch (Exception e){
e.printStackTrace();
}
return SUCCESS;
}
struts.xml
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--<constant name="struts.action.excludePattern" value="../webapp/static/*/.*?" />-->
<!-- Struts22.5strict-method-invocation()truefalse -->
<package name="default" extends="struts-default" namespace="/" strict-method-invocation="false">
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult"/>
</result-types>
<!-- CRUD -->
<action name="user_*" class="org.ssh.action.UserAction" method="{1}User">
<result name="success" type="json">
<param name="root">result</param>
<param name="callbackParameter">callback</param>
</result>
</action>
</package>
</struts>