1
我試圖創建一個api,這是我的iOS應用程序連接到的。不過,當我運行post請求並將參數設置爲id, name, birth and country
時,我不斷收到提示error: not found
。但是,如果我只是運行郵遞員url/users/14253/test/10-20-1992/denmark
它工作正常,所以我有點困惑我做錯了什麼?nodejs api和swift應用程序之間的連接
API路線
router.post('/:id/:name/:birth/:country', function(req, res, next) {
var params = req.params;
var accessHeader = req.headers;
var user = new User({
_id: params.id,
name: params.name,
birthday: new Date(params.birth),
country: params.country
});
user.save(function(err) {
if (err) {
res.send(err);
} else {
res.json({ message: 'User created!' });
}
});
});
迅速alamofire要求
func passUserData(id: String, name: String, birth: String, country: String, token: AccessToken) {
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
Alamofire.request("http://url/users", method: .post, parameters: ["id" : id, "name" : name, "birth" : birth, "country" : country], encoding: JSONEncoding.default, headers: headers).responseString { closureResponse in
if String(describing: closureResponse.result) == "SUCCESS"
{
print(closureResponse)
} else {
print(closureResponse)
}}
}
你能在你的node.js代碼中打印'params'嗎? – DrakaSAN
你不會在URL中傳遞任何參數,但在身體中。您在客戶端執行的請求具有以下網址:「/ users」與參數在正文中 – krakig
在我看來,swift代碼會以http:// IP:3000/users?id的形式生成url = bla&name = bla&etc,絕對不是你想要的網址。參數需要嵌入到url本身。 – Gimby