长期阅读,第一次提问。
我有一个应用程序可以在出现情况时发送 JSON 消息。我正在尝试让网页在收到 JSON 消息时刷新。
我是 Web 开发的新手,并使用了这个很棒的教程来开始 Get Post:
https://www.youtube.com/watch?v=aTj0ZLha1zE
我用了https://stackoverflow.com/users/88204/james-lawruk更改本地文件“data.json”时刷新网页的好答案。 How to refresh a page whenever my json data file changes
我想不通的是如何从 swift 触发刷新。 我可能在这里遗漏了一些基本的东西,但我不确定去哪里找。
我的快速代码:
@IBAction func OnPostTapped(_ sender: Any) {
let parameters = ["username": "@you", "tweet": "HelloWorld"]
guard let url = URL(string: "http://localhost:8888/data.json") else { return }
var request = URLRequest(url:url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print (response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print (json)
}catch {
print (error)
}
}
}.resume()
}
编辑
我应该澄清一下,该应用程序没有 UIWebView,它只是将 JSON 发送到网页。我正在尝试让网页在用户的浏览器上刷新。
最佳答案
我假设您在 UIWebView
中。
如果是这种情况,那么您可以执行 webView.reload();
来刷新页面。
关于javascript - 从 swift 接收 POST JSON 时刷新网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51645004/