Json 如何使用Swift和Alamofire连接http服务器
嗨,我是swift的新手,我的服务器连接有问题。我正在尝试使用post方法请求使用用户名和密码登录到服务器。如何连接到服务器并进行身份验证 错误消息:Json 如何使用Swift和Alamofire连接http服务器,json,swift,alamofire,Json,Swift,Alamofire,嗨,我是swift的新手,我的服务器连接有问题。我正在尝试使用post方法请求使用用户名和密码登录到服务器。如何连接到服务器并进行身份验证 错误消息: ["id": <null>, "error": { code = "-32700"; message = "Parse error"; }, "jsonrpc": 2.0] 我需要获取的服务器上的Json: {"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session
["id": <null>, "error": {
code = "-32700";
message = "Parse error";
}, "jsonrpc": 2.0]
我需要获取的服务器上的Json:
{"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session":"07e111d317f7c701dc4dfde1b0d4862d","timeout":300,"expires":300,"acls":{"access-group":{"superuser":["read","write"],"unauthenticated":["read"]},"ubus":{"*":["*"],"session":["access","login"]},"uci":{"*":["read","write"]}},"data":{"username":"root"}}]}
请求应该如下所示:
"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"admin01\" } ] }"
请尝试使用以下代码:
Alamofire.request(url, method: .post, parameters: parameters).responseJSON { response in
if(response.result.isFailure){
print("no data!");
}else{
print("received data!");
// make sure we got some JSON since that's what we expect
guard let json = response.result.value as? [String: Any] else {
print("didn't get todo object as JSON from API")
print("Error: \(response.result.error)")
return
}
print(json)
}
}
尝试将let param:[String:Any]更改为let param:[String:String]这两次尝试了,没有帮助您尝试在浏览器中点击您的url吗?基本上,此url是路由器,url不在浏览器上工作,但使用android和请求它的工作完美见:我得到:收到数据!这意味着这是可行的。现在把你的代码放在那里,然后我尝试打印结果json,我得到相同的错误代码=“-32700”;message=“解析错误”;};id=“”;jsonrpc=“2.0”@然后,您需要正确地检查JSON格式。Parse error表示解析JSON时出现了问题。我将在我的问题请求中添加terminal和我应该得到的JSON,你能帮我吗?
Alamofire.request(url, method: .post, parameters: parameters).responseJSON { response in
if(response.result.isFailure){
print("no data!");
}else{
print("received data!");
// make sure we got some JSON since that's what we expect
guard let json = response.result.value as? [String: Any] else {
print("didn't get todo object as JSON from API")
print("Error: \(response.result.error)")
return
}
print(json)
}
}