我是Swift的新手。试图在localhost上发布laravel的帖子请求。验证我的Request
在laravel内迅速接收到了Request
。我将JSON
作为public function verify_login(Request $request)
{
return response()->json($request)
}
回复。产生这个错误。
代码3840“JSON文本没有以数组或对象和选项开头 允许片段未设置。“
这意味着JSON响应格式错误。
Laravel UserController
@IBAction func verify_login(_ sender: UIButton) {
let username: String = self.username.text!
let passkey: String = self.passkey.text!
//print(username)
let urlString = "http://localhost:8888/user/verify"
guard let requestUrl = URL(string:urlString) else { return }
let parameters = "username=\(username)&passkey=\(passkey)"
var request = URLRequest(url:requestUrl)
request.httpMethod = "POST"
request.setValue("application/x-www-form-unlencoded;charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = parameters.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in
if error == nil,let usableData = data {
var json: [String: Any]?
do {
json = try JSONSerialization.jsonObject(with: usableData) as? [String:Any]
print(json?["username"]! as Any)
}catch{
print(error)
}
}
}
task.resume()
}
ViewController.swift
Postman
注意:
* {
margin: 0;
padding: 0;
border: none;
}
img {
max-width: 100%;
}
.navbar .navbar-collapse {
flex-basis: auto;
}
@media screen and (max-width:991px){
.navbar .navbar-collapse {
flex-basis: 100%;
}
}
我收到预期的响应(请求对象为json)。