我有这个脚本
<?php
include_once "fungsi.php";
include_once "conf/conf_Star04.php";;
header('Access-Control-Allow-Origin: *');
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(!isset($_POST['card'])){
$json = array("status" => 0,"Desc" => "Error", "message" => "Please insert CardNumber");
}else{
$card = $_POST['card'];
$email = $_POST['email'];
$address = $_POST['address'];
$hp = $_POST['hp'];
// QUERY HERE
$sql = "MY Query";
$result = sqlsrv_query( $conn, $sql );
if ($result){
$json = array("status" => 1,"Desc" => "Done");
}else{
$json = array("status" => 0,"Desc" => "Error");
}
}
}else{
$json = array("status" => 0,"Desc" => "Error");
}
?>
我制作API POST方法。该脚本有什么问题?
当我访问时,它总是给出错误未定义的变量:json
答案 0 :(得分:0)
您没有正确初始化 $ json 变量,请尝试以下代码,这可能有所帮助......
<?php
include_once "fungsi.php";
include_once "conf/conf_Star04.php";;
header('Access-Control-Allow-Origin: *');
$json = array("status" => 1);
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(!isset($_POST['card'])){
echo "Please insert CardNumber";
}else{
$card = $_POST['card'];
$email = $_POST['email'];
$address = $_POST['address'];
$hp = $_POST['hp'];
// QUERY HERE
$sql = "MY Query";
$result = sqlsrv_query( $conn, $sql );
if ($result){
$json["status"] = 1;
$json["Desc"] = "Done";
echo json_encode($json);
}else{
$json["status"] = 0;
$json["Desc"] = "Error";
echo json_encode($json);
}
}
}else{
$json["status"] = 0;
$json["Desc"] = "Error";
echo json_encode($json);
}
?>
答案 1 :(得分:0)
你的问题就在这一行 - &gt; echo "Please insert CardNumber";
在代码的最后,您正在执行echo json_encode($json);
,因此期望$json
变量。
因此,您应该更改此行:
echo "Please insert CardNumber";
到
$json = array("status" => 0,"Desc" => "Error", "message" => "Please insert CardNumber");