0
動作代碼:如何在Struts2中使用ES6獲取數據時獲取數據?
public class ContentAction extends ActionSupport {
private String menuName;
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
}
JS代碼:
import "es6-promise/auto";
import fetch from 'isomorphic-fetch'
export function postAction(action,url,params=[]) {
return function (dispatch) {
const data = params;
return fetch(url,{
method:"POST",
body:data//data=Object{menuName:"index"}
})
.then(response => response.json())
.then(json =>dispatch(action(json)))
}
}
但是當項目來看,MENUNAME是在行動無效。
我嘗試使用鍵 - 值對:
import "es6-promise/auto";
import fetch from 'isomorphic-fetch'
export function postAction(action,url,params=[]) {
return function (dispatch) {
const data = Object.keys(params).map(key =>
encodeURIComponent(key)+"="+ encodeURIComponent(params[key])).join('&');
return fetch(url,{
method:"POST",
headers:{'Content-Type':'application/x-www-form-urlencoded'},
body:data//data="menuName=index"
})
.then(response => response.json())
.then(json =>dispatch(action(json)))
}
}
使用JSON:
export function postAction(action,url,params=[]) {
return function (dispatch) {
const data = JSON.stringify(params);
return fetch(url,{
method:"POST",
headers:{'Content-Type':'application/json'},
body:data//data="{"menuName":"index"}"
})
.then(response => response.json())
.then(json =>dispatch(action(json)))
}
}
使用FORMDATA:
export function postAction(action,url,params=[]) {
return function (dispatch) {
const data = new FormData();
Object.keys(params).map(key =>
data.append(encodeURIComponent(key),encodeURIComponent(params[key])));
return fetch(url,{
method:"POST",
headers:{'Content-Type':'application/x-www-form-urlencoded'},
body:JSON.stringify(data)//data.get("menuName")="index"
})
.then(response => response.json())
.then(json =>dispatch(action(json)))
}
}
所有上面不能工作(MENUNAME =無效)
但如果我追加鍵值對直接URL,它的工作原理(MENUNAME =在行動指數):
import "es6-promise/auto";
import fetch from 'isomorphic-fetch'
export function postAction(action,url,params=[]) {
return function (dispatch) {
const data = Object.keys(params).map(key =>
encodeURIComponent(key)+"="+ encodeURIComponent(params[key])).join('&');
return fetch(url + "?" + data,{//url.action?menuName=index
method:"POST"
})
.then(response => response.json())
.then(json =>dispatch(action(json)))
}
}
,但我認爲這種方式不應該是標準的,它甚至沒有post.So在哪裏我犯了一個錯誤嗎?
經過這些天的調試,我發現問題是「後」,而不是「取」。但仍然感謝您的細緻回答 – findsky