我正在使用的iOS應用程序下面的斯威夫特3代碼,試圖更新後端MySQL數據庫:iOS版雨燕3後問題
var request = URLRequest(url: URL(string: "https://str8red.com/updateAPNS")!)
request.httpMethod = "POST"
let postString = "devicetoken=gjhgjgjgkkgggkgkghgkgkhjg"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
當我運行這段代碼的iOS作爲預期,並返回500錯誤來自Web服務器。當我檢查網絡服務器報告中,我得到以下錯誤:
MultiValueDictKeyError at /updateAPNS/
"'devicetoken'
更容易混淆的反饋表明:
POST: No POST data
是否應的iOS不是如在上面的代碼在第3行中描述張貼devicetoken變量?如果不是,我如何在我的視圖中訪問devicetoken變量?
我的Django的看法是低於櫃面它可以幫助:
def updateAPNS(request, extra_context={}):
currentUser = request.user
currentUserID = currentUser.id
if not currentUserID:
return HttpResponse("APNS is NOT Updated")
else:
APNSUpdate, created = APNSDevice.objects.update_or_create(user_id=currentUserID,
defaults={"user_id": currentUserID,
"registration_id": request.POST['devicetoken']})
return HttpResponse("APNS is Updated")
我很欣賞你的快速反應。我很新,所以你可以確認我把這個和它的一個例子叫做什麼? –
得到以下錯誤:使用未解析的標識符'postToUrl' –
錯誤,這是我上面的函數的名稱。我在編輯 – Spads