带有JSON的PHP转换为经典ASP

时间:2022-06-10 05:29:00

I am working on the login part of an android app. There is a PHP page that uses JSON to get the input from the app and queries it to the SQL database and returns if the login is succesful or not.

我正在研究Android应用程序的登录部分。有一个PHP页面使用JSON从应用程序获取输入并将其查询到SQL数据库,并在登录成功与否时返回。

Now, my boss doesn't want a PHP page and it has to be ASP.

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

How can I convert the PHP page to ASP page that uses vbscript/javascript?

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

I found http://www.aspjson.com/ Not sure how to convert the json_encode

我发现http://www.aspjson.com/不确定如何转换json_encode

PHP page code:

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(); 
?>

1 个解决方案

#1


0  

I am not a PHP expert, but I think that die() returns the string passed to it back to the client and stops the script.

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

I think the equivalent ASP would be

我认为等效的ASP会是

response.write json_encode(responseObject)
response.end

So then the question is how to write json_encode.

那么问题是如何编写json_encode。

Personally, given this exact example, I would simplify the task by hand-coding the JSON response:

就个人而言,鉴于这个确切的例子,我将通过手工编写JSON响应来简化任务:

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

This avoids the necessity of figuring out how to implement a general-purpose JSON encoding method to do this very simple thing. You could make it slightly more general-purpose by writing a method that takes a success code and a message string and returns a JSON string if you want.

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

#1


0  

I am not a PHP expert, but I think that die() returns the string passed to it back to the client and stops the script.

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

I think the equivalent ASP would be

我认为等效的ASP会是

response.write json_encode(responseObject)
response.end

So then the question is how to write json_encode.

那么问题是如何编写json_encode。

Personally, given this exact example, I would simplify the task by hand-coding the JSON response:

就个人而言,鉴于这个确切的例子,我将通过手工编写JSON响应来简化任务:

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

This avoids the necessity of figuring out how to implement a general-purpose JSON encoding method to do this very simple thing. You could make it slightly more general-purpose by writing a method that takes a success code and a message string and returns a JSON string if you want.

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