简体   繁体   English

使用json数据类型和Ajax从php文件中检索内容

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

I have used ajax post with datatype json to fetch content from a php file and display. 我已经使用数据类型为json的ajax post从php文件中获取内容并显示。 But I'm new to json and I was unable to get content from the php file. 但是我是json的新手,我无法从php文件中获取内容。 only numeric value is fetched. 仅获取数值。 Here is the code I'm using 这是我正在使用的代码

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);
});

I used the above function to get content from the php file and alerted. 我使用上述功能从php文件中获取内容并发出警报。 But alert not working.Kindly guide what iam doing wrong.Here is my php code 但是警报不起作用。请指导我做错了。这是我的PHP代码

events_fetch.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;

edit your events_fetch.php as below 如下编辑您的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