我是我的快捷应用程序.我想用alamofire创建一个请求POST
请求
$ curl \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-X POST -d '{ "user": { "email": "user@example.com", "password": "1234" } }' \
http://localhost:3000/users/sign_in
Run Code Online (Sandbox Code Playgroud)
我的代码
let crendetials = ["user": ["email": "bolo@me.com", "password": "PASSWORD"]]
var postsEndpoint: String = "http://localhost:3000/users/sign_in"
Alamofire.request(.POST, postsEndpoint, parameters: crendetials, encoding: .JSON)
.responseJSON { (request, response, data, error) in
if let anError = error
{
// got an error in getting the data, need to handle it
println("error calling POST on /users/sign_in")
println(error)
}
else if let data: AnyObject = data
{
// handle the results as JSON, without a bunch of nested if loops
let post = JSON(data)
// to make sure it posted, print the results
println(data)
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个错误
request: <NSMutableURLRequest: 0x170208be0> { URL: http://localhost:3000/users/sign_in } || response: nil || object: Optional(<>) || error: Optional(Error Domain=NSURLErrorDomain Code=-1004 "The operation couldn’t be completed. (NSURLErrorDomain error -1004.)" UserInfo=0x1740f6900 {NSErrorFailingURLStringKey=http://localhost:3000/users/sign_in, NSErrorFailingURLKey=http://localhost:3000/users/sign_in, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSUnderlyingError=0x17405c500 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1004.)"})
Run Code Online (Sandbox Code Playgroud)
如果您正在使用真正的iPhone进行测试,则应将主机更改为localhostMac的IP地址,并确保iPhone和Mac位于同一无线网络中.
如果您正在使用模拟器进行测试,它应该可以正常工作.也许你可以换localhost到127.0.0.1.
该错误是由于错误的网址造成的.您可以在设备的Safari中测试它是否有效.
| 归档时间: |
|
| 查看次数: |
2110 次 |
| 最近记录: |