![](/img/trans.png)
[英]Returning Uploadifive Ajax 'Error/Success' data from Codeigniter Controller
[英]Returning data from controller - Codeigniter
我正在嘗試將數據返回到我的變量 $item_id 但變量返回空。 我沒有以正確的方式返回數據嗎?
item_id 應返回來自 serverMonitor.php 中索引 function 的“警報”。
請指教。 謝謝
項目.php
function rest_state()
{
$id = 1;
$item_id = $this->pass_to_res($id);
return $item_id //this should return "Alert"
}
function pass_to_res($id)
{
$url = "https://example.com/serverMonitor?id=$id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
serverMonitor.php
function index() {
$id = htmlentities($_GET['id']);
$word = "Alert";
return $word;
}
來自 function 的返回值並不總是對訪問者可見。 您應該使用 echo、print 或 print_r。
function index() {
$id = htmlentities($_GET['id']);
$word = "Alert";
echo $word;
return true;
}
您還可以使用 JSON 和 json_encode 進行編碼
function index() {
$id = htmlentities($_GET['id']);
$word = "Alert";
echo json_encode(['word' => $word]);
return true;
}
和 json_decode 來解碼:
function pass_to_res($id)
{
$url = "https://example.com/serverMonitor?id=$id";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
curl_close ($ch);
$result = json_decode($result);
return isset($result['word']) ? $result['word'] : false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.