I want to post some data to my php page, I've tested two ways to do so but non of them worked :
let parameters = [
"name": "test",
]
Alamofire.request(URL(string: "http://www.tameshkshop.ir/11.php")!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response) in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
let responseString = String(data: response.data!, encoding: .utf8)
print(responseString)
}
the second way :
var request = URLRequest(url: URL(string: "http://www.tameshkshop.ir/11.php")!)
request.httpMethod = "POST"
let postString = "name=\(FinallFactorViewController.name)"
request.httpBody = postString.data(using: .)
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=\(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 = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
let index = responseString?.index((responseString?.startIndex)!, offsetBy: 4)
let done = responseString?.substring(to: index!)
in the php page, I get the posted value and echo them like this :
echo $_POST['name'];
but it I get nothing in return .
what is wrong ? where am I doing is wrong ?