簡體   English   中英

角度2。 HTTP.POST不起作用

[英]Angular2. HTTP.POST doesn't work

這是我的HTTP服務:

    postData(name, country) {
  var body = JSON.stringify({
    "name":name,
    "country":country
  });
  console.log(name);     // it outpus the correct value
  console.log(country);  // it outpus the correct value
  return this.http.post('http://178.62.58.150:5000/api/cosmo/',body)
    .map(res => {
      res.text();
      console.log(res.text());

    })
    .do(data => {
      this.data = data;
      // console.log(this.data);
    })
}

似乎它不會將正文發送到POST請求。

這些輸出正確的身體價值

console.log(name);     
console.log(country); 

console.log(res.text());

它給了我一個DB驗證錯誤,該錯誤指出必須輸入正文國家/地區 ...

如何正確發送屍體?

PS:我用POSTMAN測試了它,它可以工作!

看起來問題出在建立post params中,嘗試

var body = JSON.stringify({
  name: name,
  country: country
})

請注意,我的哈希鍵不是字符串,因為stringify會將JavaScript對象轉換為JSON字符串。

如果這不起作用,請使用Google Developer工具檢查您向服務器發送的請求。

我已經添加了

let headers = new Headers();
 headers.append("Content-Type","application/json");

return this.http.post('http://178.62.58.150:5000/api/cosmo/',body,{headers:headers})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM