我正在尝试快速发布请求。我在Objective-C中成功完成,但是使用Swift却无法发送请求。 PHP中的$emailOK无法获取信息。问题出在哪里?谢谢你的帮手。这是快速代码

func httpPost1(url:String, postData: String, completion: String -> Void) {
        let request = NSMutableURLRequest(URL: NSURL(string: url)!)
        request.HTTPMethod = "POST"
        let postString = postData
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
            guard error == nil && data != nil else {                                                          // check for fundamental networking error
                print("error=\(error)")
                return
            }

            if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()
        }


这是执行代码

httpPost1("xxxxx", postData: "emailOK=Hello") { result in

                print(result)//result is your string-response from server

            }


这是我的PHP代码

<?php

$link = mysqli_connect($dbhost, $username, $dbpass, $database);

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;


$emailOK = isset($_GET["emailOK"]) ? $_GET["emailOK"] : '';

echo $emailOK;

$query = "INSERT INTO UserInfo VALUES ('', '$emailOK')";
mysqli_query($link, $query) or die (mysqli_error("error"));


mysqli_close($link);
?>

最佳答案

我发现了问题,感谢@OOPer的评论。问题出在PHP $emailOK = isset($_GET["emailOK"]) ? $_GET["emailOK"] : '';中,应该是$emailOK = $_POST['emailOK'];

关于php - 无法快速发布请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38460624/

10-11 23:27