Alamofire不会执行该URL

时间:2017-10-02 06:30:16

标签: ios node.js swift alamofire

我有一个服务器在节点中运行,我尝试使用postman并且它工作正常,但是当我从IOS客户端执行它时,它没有执行URL,我检查了我的服务器它没有甚至称为API

节点服务器

router.post('/api/accounts/login', (req, res, next) => {
   var user = new User():
   user.name = req.body.name;
   user.pass = req.body.password;
   user.save((err) => {
     res.json("Success");
   });
});

Alamofire

let mainURL = "https://examplelogin.com"
let facebookLogin = "\(mainURL)/api/accounts/login"

func loginButtonTapped(_ name: String, _ pass: String) {

        let parameters: [String: AnyObject] = [
            "name": name as AnyObject,
            "password": pass as AnyObject
        ]

        Alamofire.request(facebookLogin, method: .post, parameters: parameters, encoding: JSONEncoding.default)
            .responseJSON { response in
                if let value = response.result.value {
                    let json = JSON(value)

                    // Do something


                }
        }

}

更具体地说,我在Heroku上托管了我的节点服务器,我使用namecheap来获取完整的域名,但是为了某种方式我的ios客户端没有执行API,我是否需要在Info上添加任何内容的.plist?

3 个答案:

答案 0 :(得分:1)

试试这个

1.为显示错误添加此代码。

else if let ERROR = response.result.error { 
   print(ERROR.localizedDescription)
}

2.在info.plist文件中添加此代码。

<key>NSAppTransportSecurity</key>
<dict> 
  <key>NSAllowsArbitraryLoads</key> <true/> 
</dict> 

答案 1 :(得分:0)

Try this code ...

func loginButtonTapped(_ name: String, _ pass: String) {

    let parameters = [
                "name": name,
                "password": pass
            ]
            Alamofire.request(facebookLogin, method: .post, parameters: parameters as [String : AnyObject], encoding: JSONEncoding.default)
                .responseJSON { response in
                    if let value = response.result.value {
                        let json = JSON(value)

                        // Do something

                    }
            }

    }

答案 2 :(得分:-1)

我有同样的问题,但我已经解决了这个问题: -

让jsonData =试试! JSONSerialization.data(withJSONObject:dict(要传递的包含参数的字典),选项:JSONSerialization.WritingOptions.prettyPrinted)

    let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)! as String

    Alamofire.request(url, method: .post, parameters: jsonString, encoding:JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
        switch(response.result)
        {

        }
    }