我遇到了一点问题。我有一个从文本输入获取值的结构设置:
struct CheckoutData: Codable {
var firstName: String
var lastName: String
var email: String
var streetAddress: String
var streetAddress2: String
var city: String
var state: String
var zipCode: String
var total: Double
}
然后我初始化它并对其进行编码:
let checkoutData = CheckoutData(
firstName: firstName.trimmingCharacters(in: .whitespacesAndNewlines),
lastName: lastName.trimmingCharacters(in: .whitespacesAndNewlines),
email: email.trimmingCharacters(in: .whitespacesAndNewlines),
streetAddress: streetAddress.trimmingCharacters(in: .whitespacesAndNewlines),
streetAddress2: streetAddress2.trimmingCharacters(in: .whitespacesAndNewlines),
city: city.trimmingCharacters(in: .whitespacesAndNewlines),
state: state.trimmingCharacters(in: .whitespacesAndNewlines),
zipCode: zipCode.trimmingCharacters(in: .whitespacesAndNewlines),
total: total
)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let encoded = try! encoder.encode(checkoutData)
var request = URLRequest(url: URL(string: "https://www.MyApp.com/file.php")!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = encoded
URLSession.shared.dataTask(with: request) { data, response, error in }.resume()
这是处理发送电子邮件的 PHP:
$emailto = "myemail@gmail.com";
$subject = "Mobile Order";
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["email"];
$streetAddress = $_POST["streetAddress"];
$streetAddress2 = $_POST["streetAddress2"];
$city = $_POST["city"];
$state = $_POST["state"];
$zipCode = $_POST["zipCode"];
$total = $_POST["total"];
然而,当我收到电子邮件时,所有变量都是空白的。我已经打印了编码版本,它似乎编码正确,因此它很可能位于 PHP 端。抱歉,这有点代码转储,但我无法弄清楚这一点。非常感谢!
明月笑刀无情