在Laravel内置的API中连接了Swift,但是无法获取数据
我已经使用它GET并成功地带来了数据,但是当我使用POST时无法获取数据
xCode中出现的错误
此路由不支持GET方法。支持的方法:POST
快捷代码
let session = URLSession.shared
var request = URLRequest(url: URL(string: "https://mydomin.com/index.php/api/profile/1/")!)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: [], options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
completion(nil, error)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request, completionHandler: { data, response, error in
guard error == nil else {
completion(nil, error)
return
}
guard let data = data else {
completion(nil, NSError(domain: "dataNilError", code: -100001, userInfo: nil))
return
}
do {
guard let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else {
completion(nil, NSError(domain: "invalidJSONTypeError", code: -100009, userInfo: nil))
return
}
completion(json, nil)
} catch let error {
completion(nil, error)
}
})
task.resume()
Laravel代码:
/myWebSite/routes/api.php
Route::post('/profile/{userID}','profile@show');
/myWebSite/app/Http/controllers/profile.php
public function show($userID){
$users = new User();
return $users->find($userID);
}
答案 0 :(得分:0)
我解决了编写链接的问题,因此在Swift代码中将https://mydomin.com/index.php/api/profile/1/修改为https://mydomin.com/index.php/api/profile/1并成功
谢谢我