0
我想發送POST
方法到localhost
其中body
部分內容部分。從vue
文件我的代碼是:Vue.js post方法體始終是空的
methods: {
login(){
var data = {
username: 'admin',
password: 'admin'
}
this.$http.post("http://localhost:8081/login", data)
.then(response => {
console.log(response)
})
}
}
我使用Spring-boot目錄服務器和從實施AbstractAuthenticationProcessingFilter
類attemptAuthentication
方法有斷點。對於檢查body
我用:
String myBody= httpServletRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
但這String
始終是空的(斷點作品)。我嘗試將一些值手動設置爲post
方法,如:
this.$http.post("http://localhost:8081/login", "exampleData")
但值仍爲空。
當我從郵差發送相同的方法 - 它工作得很好 - myBody
包含正確的值。
我的代碼有什麼問題?
@Update
1)爲原料和JSON我配置郵差:在我的java類
myBody = {"username":"admin","password":"admin"}
myContentType = application/json
Java類看起來像:
myBody =
------WebKitFormBoundarykeEtHWmW4BBv2y0t
Content-Disposition: form-data; name="username"
admin
------WebKitFormBoundarykeEtHWmW4BBv2y0t
Content-Disposition: form-data; name="password"
admin
------WebKitFormBoundarykeEtHWmW4BBv2y0t--
@Update
我添加標題爲我post
方法:
methods: {
login(){
this.$http.post("http://localhost:8081/login", "exampleData", {
headers: {
'content-type' : 'application/json'
}
})
.then(response => {
console.log(response)
})
}
}
但在Java類myBody
是空的,content-type
是null
。在控制檯中沒有錯誤(在瀏覽中)
我編輯我的職務 – littlewombat
如果你使用'形式,data'來傳輸數據,你應該確保瀏覽器的發佈請求的內容類型='application/x-www-form-urlencoded; charset = utf-8' – Surmon
我的解決方案是使用'application/json'類型。 – Surmon