【问题标题】:PHP with JSON converted to classic ASP带有 JSON 的 PHP 转换为经典 ASP
【发布时间】:2015-11-30 09:28:20
【问题描述】:

我正在开发一个安卓应用程序的登录部分。有一个 PHP 页面使用 JSON 从应用程序获取输入并将其查询到 SQL 数据库并返回登录是否成功。

现在,我的老板不想要 PHP 页面,它必须是 ASP。

如何将 PHP 页面转换为使用 vbscript/javascript 的 ASP 页面?

我找到了http://www.aspjson.com/ 不知道如何转换 json_encode

PHP页面代码:

 <?php mysql_connect("localhost","user","pass"); 
$db= mysql_select_db("database"); 
$androidID=$_POST["androidID"]; 
$username=$_POST["username"]; 
if (!empty($_POST)) { 
if (empty($_POST['username']) || empty($_POST['androidID'])) { 
// Create some data that will be the JSON response 
$response["success"] = 0; 
$response["message"] = "One or both of the fields are empty ."; 

//die is used to kill the page, will not let the code below to be executed. It will also
//display the parameter, that is the json data which our android application will parse to be 
//shown to the users 
die(json_encode($response)); 
} 
$query = " SELECT id, username, aid FROM login WHERE username = '$username'and aid='$androidID'"; 
$sql1=mysql_query($query); 
$row = mysql_fetch_array($sql1); 
if (!empty($row)) { $response["success"] = 1; 
$response["message"] = "You have been sucessfully login"; 
die(json_encode($response)); 
} 
else{ 
$response["success"] = 0; $response["message"] = "invalid username or password "; 
die(json_encode($response)); 
}
} 
else{ $response["success"] = 0; 
$response["message"] = " One or both of the fields are empty "; 
die(json_encode($response)); 
} 

mysql_close(); 
?>

【问题讨论】:

    标签: javascript php json vbscript asp-classic


    【解决方案1】:

    我不是 PHP 专家,但我认为 die() 会将传递给它的字符串返回给客户端并停止脚本。

    我认为等效的 ASP 将是

    response.write json_encode(responseObject)
    response.end
    

    那么问题是如何写json_encode

    就个人而言,给出这个确切的例子,我会通过手动编码 JSON 响应来简化任务:

    response.write "{success:1, message:""You have been successfully login""}"
    response.end
    

    这避免了弄清楚如何实现通用 JSON 编码方法来完成这个非常简单的事情的必要性。您可以通过编写一个方法来使其更通用,该方法采用成功代码和消息字符串并根据需要返回 JSON 字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 1970-01-01
      相关资源
      最近更新 更多