gpt4 book ai didi

jquery - 使用 SWIFT、iOS 进行 JSON 查询

转载 作者:行者123 更新时间:2023-11-30 13:28:22 26 4
gpt4 key购买 nike

我想快速转换此查询:

<script>
jQuery.ajax(
{
url: "http://website.net/api/Clients/",
type: 'POST',
headers: {"Token": "Votre token"},
dataType: "json",
data: { "PhoneNumber": phoneNumberValue, "SecretCode": secretCodeValue },
success: function (data) { alert("it worked!") }
});
<script>

我在 Swift 中尝试过,但没有成功:

func submitNumber() {
let session = NSURLSession.sharedSession()
let url = NSURL(string: "http://website.net/api/Clients/")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST”
request.allHTTPHeaderFields = ["Token" : “myKey”]
let requestDictionary = ["PhoneNumber": 3333333333, "SecretCode": 12345]

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(requestDictionary, options: [])

let task = session.dataTaskWithRequest(request) { data, response, error in

if let httpResponse = response as? NSHTTPURLResponse {
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

print(json)

}catch {
print(error)
}


} else {
print(“failure”)
}
}
}
task.resume()
}

但是总是打印“失败”,我不知道哦如何处理......

请问我该怎么做?非常感谢!

最佳答案

查看我对 this 的回答问题。您没有将请求的内容类型设置为接受 json。

关于jquery - 使用 SWIFT、iOS 进行 JSON 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36866627/

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