2017-02-18 84 views
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目錄服務器和從實施AbstractAuthenticationProcessingFilterattemptAuthentication方法有斷點。對於檢查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類

enter image description here 是:

myBody = {"username":"admin","password":"admin"} 
myContentType = application/json 

2)形式: enter image description here

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-typenull。在控制檯中沒有錯誤(在瀏覽中)

回答

0

你應該檢查你的服務器是否得到請求的bodyform

像這樣:

enter image description here

檢查布勞爾PARAMS像這樣:

enter image description here

+0

我編輯我的職務 – littlewombat

+0

如果你使用'形式,data'來傳輸數據,你應該確保瀏覽器的發佈請求的內容類型='application/x-www-form-urlencoded; charset = utf-8' – Surmon

+0

我的解決方案是使用'application/json'類型。 – Surmon