0
在搜索堆棧並查看類似問題後的數小時後,我仍然無法使其工作。請幫助指導我!Json向服務器發送郵件請求時出錯
所有我試圖做的是提交的用戶名,以我的服務器,並將其保存在MySQL的 - 只是爲了得到它的工作...
我可以通過發送用戶名到服務器,甚至返回硬編碼數據回到iOS模擬器設置標籤的文本,,但只要我添加保存數據到MySQL的函數,我得到錯誤:「錯誤域= NSCocoaErrorDomain代碼= 3840」字符0周圍的值無效。「UserInfo = { NSDebugDescription =字符0周圍的值無效}}
對不起,我錯誤的命名約定,我偏離了教程。謝謝!
SWIFT 3:
func Apitest(name: String) {
let json = ["user" : name]
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let url = NSURL(string: "I put the actual path here, don't worry")!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
if error != nil{
print("First Error -> \(error)")
return
}
do {
let result = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print("Result -> \(result!)")
let label = result?["messages"] as! String
print(label)
self.setupLabel(text: label)
} catch {
print("Second Error -> \(error)")
}
}
task.resume()
} catch {
print(error)
}
}
API代碼(蟒/瓶)
@app.route('/api/get_messages/') methods=['POST'])
def get_messages():
json = request.get_json()
api_test(json['user'])
return jsonify({'messages':json['user']})
MySQL的功能:
def api_test(user):
c, conn = connection()
c.execute("INSERT INTO users (email) VALUES (%s)", (user))
conn.commit()
conn.close()
c.close()
對不起,實際上是這樣的。我沒有粘貼該行,我輸入了它。 – user3344239