【问题标题】:JSON from PHP file result was code, do not textPHP 文件结果中的 JSON 是代码,而不是文本
【发布时间】:2015-08-27 02:51:47
【问题描述】:

我在文件 update_customer.php 中有代码 PHP 来保存客户信息

    <?php
date_default_timezone_set('Asia/Ho_Chi_Minh');

$response = array( 'status' => 0, 'message' => '', 'typeinsurance' => '', 'thoigianmua' => 0 );

if (empty($_GET) && !empty($_POST) && count($_POST) == 7 && isset($_POST['fullname']) && isset($_POST['email']) && 
    isset($_POST['address']) && isset($_POST['phone']) && isset($_POST['cost']) && isset($_POST['typeinsurance']) && 
    isset($_POST['typepay']))
{
    require_once ('./../include/database.php');
    $database = new Database();

    $time = time();

    $_POST['thoigianmua'] = $time;

    if ($database->insert('customer', $_POST)){
        $response['status'] = 1;
        $response['typeinsurance'] = $_POST['typeinsurance'];
        $response['thoigianmua'] = $time;
    }
}

echo json_encode($response);

并编码 JQUERY 以发送数据,但我的 JSON 得到

Object {status: 1, message: "", typeinsurance: "<?php echo $_POST['typeinsurance']; ?>", thoigianmua: 1440642246}

请帮助我,在此先感谢

【问题讨论】:

  • 向我们展示生成此输出的 PHP 代码。
  • 您能否展示一个您期望的 JSON 样本以及应该生成它的字段的输入?
  • 这是我的 PHP 代码$response['status'] = 1; $response['typeinsurance'] = $_POST['typeinsurance']; $response['thoigianmua'] = $time;
  • 据我所知,您似乎正在通过帖子字段 typeinsurance 发送实际字符串 &lt;?php echo $_POST['typeinsurance']; ?&gt;,但我发现很难将您的代码与输出相关联 - 您能否发布更多实际的代码生成响应和发送请求的代码?
  • 我要显示 JSON Object {status: 1, message: "", typeinsurance: "xehaibanh", thoigianmua: 1440642246

标签: php jquery json


【解决方案1】:

您的 php 代码肯定有错误,这就是它以您意想不到的方式返回数据的原因。

错误的部分是&lt;?php echo $_POST['typeinsurance']; ?&gt;你已经将php代码作为字符串给出。

你必须检查你的 php 文件

【讨论】:

  • 这是我的代码 PHP &lt;?php date_default_timezone_set('Asia/Ho_Chi_Minh'); $response = array( 'status' =&gt; 0, 'message' =&gt; '', 'typeinsurance' =&gt; '', 'thoigianmua' =&gt; 0 ); require_once ('./../include/database.php'); $database = new Database(); $time = time(); $_POST['thoigianmua'] = $time; if ($database-&gt;insert('customer', $_POST)){ $response['status'] = 1; $response['typeinsurance'] = $_POST['typeinsurance']; $response['thoigianmua'] = $time; } echo json_encode($response);
  • 我没有投反对票,但可能是因为你的回答没有任何东西可以帮助他解决问题。
  • @MrKen 不要尝试将代码放入 cmets。编辑问题。
  • 我把代码放在了有问题的地方,并有代码通知,但不允许
  • @MrKen 问题中已有代码,可以添加更多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多