簡體   English   中英

使用json數據類型和Ajax從php文件中檢索內容

[英]Retrieve contents from a php file using json data type and ajax

我已經使用數據類型為json的ajax post從php文件中獲取內容並顯示。 但是我是json的新手,我無法從php文件中獲取內容。 僅獲取數值。 這是我正在使用的代碼

var jqxhr = $.ajax({
  type: 'POST',
  url: 'events_fetch.php',
  data: {eventID:calEvent.id,action:'LOAD_SINGLE_EVENT_BASED_ON_EVENT_ID'},
  dataType: 'json'
})
.done(function(ed) { 
  alert(ed);
});

我使用上述功能從php文件中獲取內容並發出警報。 但是警報不起作用。請指導我做錯了。這是我的PHP代碼

events_fetch.php

$eventID= mysql_real_escape_string($_REQUEST["eventID"]);
$qry="SELECT * FROM `events` WHERE `id`='$eventID'";
$res=mysql_query($qry);
$fetch=mysql_fetch_array($res);

$strtdate=$fetch['start-date'];
$strttime=$fetch['start-time'];


$retValue=array($strtdate,$strttime);
echo $retValue;

如下編輯您的events_fetch.php

$eventID= mysql_real_escape_string($_REQUEST["eventID"]);
$qry="SELECT * FROM `events` WHERE `id`='$eventID'";
$res=mysql_query($qry);
$fetch=mysql_fetch_array($res);

$strtdate=$fetch['start-date'];
$strttime=$fetch['start-time'];


$retValue=array($strtdate,$strttime);
echo json_encode($retValue);

您必須在輸出上應用json_encode函數:

echo json_encode($retValue);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM