【发布时间】:2019-08-21 05:23:51
【问题描述】:
我使用 vuejs-axios 从客户端获取数据并将其发布到服务器端。在服务器端,我使用了 java wherby,我使用 SparkJAVA 框架来处理请求-响应。 下面提到的代码块是我用来将 Form-Data 从 vuejs 发布到 java 的。
const formData = new FormData();
formData.append("name", this.name);
formData.append("address", this.address);
console.log(formData);
axios
.post("http://localhost:1234/formData", formData)
.then(response => {
console.log(response);
})
.catch(err => {
console.log(err);
});
以下代码用于解析从vuejs获取的FormData。
public static void main(String[] args) {
Spark.port(1234);
Main.apply();
get("/", (req, res) -> {
return new ModelAndView(null, "StartPage.ftl");
}, new FreeMarkerEngine());
post("/formData", (req, res) -> {
return FormController.showFormData(req, res);
});
}
public class FormController {
public static Logger logger = LogManager.getLogger(FormController.class);
public static String showFormData(Request req, Response res) {
System.out.println("Data body: " + req.body());
System.out.println("Name: " + req.queryParams("name"));
System.out.println("Address: " + req.queryParams("address"));
return "Well Done!!!";
}
}
下面提到的是输出:
Content-Disposition: form-data; name="name"
Daniel
------WebKitFormBoundarydSA5AgIIhlMNCqA2
Content-Disposition: form-data; name="address"
New York
------WebKitFormBoundarydSA5AgIIhlMNCqA2--
Name: null
Address: null
我在 req.body() 中得到响应,但在 req.queryParams() 中没有。为什么? 此外,req.body() 的响应看起来有点不寻常。
【问题讨论】:
-
看起来像 多部分表单数据
-
你能解释一下吗?我浏览了多部分表单数据,但无法解决我的问题。
标签: java node.js vue.js axios spark-java