doubi6898 2017-05-18 00:08
浏览 77

Alamofire POST请求PHP脚本没有值

I have a problem with a POST request in Alamofire. If I try to make a POST Request, it seems that the PHP script doesn't get the values. This is my POST Request:

func userRegistration(){
    let url = "http://offlineshopper.sunpowr.de/register.php"
    let parameters: [String:String] = ["nickname":RegisterTexts.nickname, "password":RegisterTexts.password, "email":RegisterTexts.email]
    print(parameters)
    Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON{ response in
        if((response.result.value) != nil){
            let swiftyJsonVar = JSON(response.result.value!)
            print("Das kommt dabei raus: \(swiftyJsonVar)")
        }
    }
}

The parameters Dictionary print this: ["nickname": "Test", "password": "Test", "email": "Test"]

So I think this is right.

This here is my PHP Script:

<?php
include("mysql_connect.php");

if(isset($_POST['nickname'])){
    $nickname = $_POST['nickname'];
}

if(isset($_POST['password'])){
    $password = $_POST['password'];
}

if(isset($_POST['email'])){
    $email = $_POST['email'];
}

//Check if nickname is already used
$select = "SELECT `nickname` FROM `User` WHERE nickname = '$nickname'";
$result = mysqli_query($link, $select);
if (!$result){
    $returnArray["message"] = "Fehler in der Datenbank!";
    json_encode($returnArray);
}
if (mysqli_num_rows($result) != 0){
    $returnArray["message"] = "Nickname bereits vorhanden!";
    json_encode($returnArray);
}

//Add new User to the database
$sql = "INSERT INTO User (nickname, password, email) VALUES ('$nickname', '$password', '$email')";
if(mysqli_query($link, $sql)){
    $returnArray["message"] = "Erfolgreich registriert!";
    echo json_encode($returnArray);
}
else{
    mysqli_error($link);
    $returnArray["message"] = "Fehler!";
    echo json_encode($returnArray);
}

mysqli_close($link)
?>

I get the response "Erfolgreich registriert" in my App so everything should work fine. I also get new datasets in my database. The problem is that only the id is set in the database, which is auto increment. The other fields are empty so the POST variables are never saved in the database.. Any ideas what I'm doing wrong here?

EDIT: It's working now, just switching

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON

with

Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseJSON
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型