gpt4 book ai didi

json - "login"和 "sign up"Web 服务未使用 alamofire 快速提供所需的 json 输出

转载 作者:行者123 更新时间:2023-12-03 11:24:10 25 4
gpt4 key购买 nike

1)

我是使用 alamofire 的新手。这是我尝试使用 alamofire 进行 Web 服务检查我错的地方。我在 loginviewcontroller.swift 中进行了登录 Web 服务如下

let url="http://192.169.201.32:9000//users/authenticate"
override func viewDidLoad() {
super.viewDidLoad()

Alamofire.request(url, method: .post, parameters:["username":"andrews","password":"admin2"], encoding: URLEncoding.default)
.responseJSON { response in
print("abcsign in")
print(response)
print("abcsign in3")
print(response.result)
//to get status code
if let status = response.response?.statusCode {
switch(status){
case 201:
print("example success")
default:
print("error with response status: \(status)")
}
}
//to get JSON return value
if let result = response.result.value {
let JSON = result as! NSDictionary
print("abcsign in 2")
print(JSON)

}

}

打印(响应)

失败

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 2." UserInfo={NSDebugDescription=Invalid value around character 2.}))


print(response.result)

失败

error with response status: 404



2)

第二个 signUpviewcontroller.swiftsignUp 连接 View Controller 。在 signUpViewController.swift singUp webservice的代码如下
  let url="http://192.169.201.32:9000//patient/signUp"

@IBAction func signUpButtonWasPressed(_ sender: Any) {
Alamofire.request(url, method: .post, parameters:["dob":DateOfBirthTextFeild.text ,
"email":emailIdTextField.text ,
"firstName":FirstNameTextField.text ,
"gender":genderTextField.text ,
"lastName":LastNameTextField.text ,
"middleName":MiddleNameTextField.text ,
"password":passwordTextField.text , //password must be 8 char long
"ssn":ssnTextField.text], encoding: URLEncoding.default)
.responseJSON { response in
print("abcsig up in")
print(response)
print("abcsign up in3")
print(response.result)
//to get status code
if let status = response.response?.statusCode {
switch(status){
case 201:
print("example success")
default:
print("error with response status: \(status)")
}
}
//to get JSON return value
if let result = response.result.value {
let JSON = result as! NSDictionary
print("abcsign up in 2")
print(JSON)

}

}
}

打印(响应)输出是

失败

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 2." UserInfo={NSDebugDescription=Invalid value around character 2.}))


print(response.result) 

error with response status: 404



如何获得有效的 json 响应?

您可以从此链接下载项目。 https://drive.google.com/file/d/1WjEng8RtRcuPOccTUHchDELIE1JSYlNW/view?usp=sharing
在 postman 上,api 显示了所需的 json 输出。
enter image description here

在正文中提供参数。您可以添加将 json url 请求转换为 post 的 json 参数。在下面的选项卡中,选择 body 通过选择 raw 将参数放在这里。
enter image description here

enter image description here

最佳答案

我用 postman 做了同样的测试,见下图。可能是验证 URL 或来自支持的开发的问题。所以请提供更多信息给你正确的答案。

enter image description here
enter image description here

关于json - "login"和 "sign up"Web 服务未使用 alamofire 快速提供所需的 json 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com