无法在Swift应用程序上分析服务器PHP

无法在Swift应用程序上分析服务器PHP,php,ios,json,swift,parsing,Php,Ios,Json,Swift,Parsing,我正在尝试在我的iOS应用程序上实现注册/登录功能。 为此,我编写了一个简单的服务器端php脚本,只是为了检查它是否返回值。 以下是我的php代码: <?php if(isset($_POST["username"])){ $username = $_POST["username"]; $password = $_POST["password"]; if($username == "admin" && $password == "admin") { $details; $

我正在尝试在我的iOS应用程序上实现注册/登录功能。 为此,我编写了一个简单的服务器端php脚本,只是为了检查它是否返回值。 以下是我的php代码:

<?php

if(isset($_POST["username"])){
$username = $_POST["username"];
$password = $_POST["password"];
if($username == "admin" && $password == "admin")
{
$details;
$details['success'] = $username;
echo json_encode($details);
}
else{
echo "invalid credential";
}
};
?>
但是,我得到了以下错误:

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

并且我的响应数据是无效的凭据。

您的php脚本对于Web服务来说不完整。它没有返回json。您必须首先完成您的Web服务。我不是一个php的家伙。但我还是试图改进剧本:

<?php
if(isset($_POST["username"])){
$username = $_POST["username"];
$password = $_POST["password"];
if($username == "admin" && $password == "admin")
{
    $details;
    $details['success'] = $username;
    return json_encode($details);
}
else{
    return json_encode( array("response" => "invalid credential") );
}
};
?>

谢谢,我明天会试试,让你知道。是的,我确信错误在swift代码中。我搞不懂它是什么,你为什么不用阿拉莫菲尔呢。默认情况下,它处理jSon。我们不需要关心它。只需将库集成到您的项目中。下面是操作说明:我们,我将php文件更改为我的项目所需的文件,并且它正在工作。不管怎样,谢谢你。考虑到php脚本本身并没有错。从应用程序发送的请求必须以不同的方式运行。新的php代码涉及大量数据库连接和其他内容。我朋友给了我那个文件。然而,在Swift代码中,我做了1个更改。我将json解析代码放在响应块中。
<?php
if(isset($_POST["username"])){
$username = $_POST["username"];
$password = $_POST["password"];
if($username == "admin" && $password == "admin")
{
    $details;
    $details['success'] = $username;
    return json_encode($details);
}
else{
    return json_encode( array("response" => "invalid credential") );
}
};
?>
let parameters = ["username": usernameField.text! ,"password" : passwordField.text! ]

Alamofire.request(.POST, "http://localhost/app/index.php", parameters: parameters)
     .responseString { response in
         print("Response String: \(response.result.value)")
     }
     .responseJSON { response in
         print("Response JSON: \(response.result.value)")
       //Handle the json response here
      if let parseJSON = response.result.value {
                let firstNameValue = parseJSON["success"] as? String
                print("Username: \(firstNameValue!)")
            }
     }