News新闻

业界新闻动态、技术前沿
Who are we?

您的位置:首页      乐道系统FAQ      ajax调用返回php接口返回json数据的方法(必看篇)

ajax调用返回php接口返回json数据的方法(必看篇)

发布日期:2017-05-05 00:00:00 110

php代码如下:

<?php

  header('Content-Type: application/json');
  header('Content-Type: text/html;charset=utf-8');

  $email = $_GET['email'];

  $user = [];

  $conn = @mysql_connect("localhost","Test","123456") or die("Failed in connecting database");
  mysql_select_db("Test",$conn);
  mysql_query("set names 'UTF-8'");
  $query = "select * from UserInformation where email = '".$email."'";
  $result = mysql_query($query);
  if (null == ($row = mysql_fetch_array($result))) {
    echo $_GET['callback']."(no such user)";
  } else {
    $user['email'] = $email;
    $user['nickname'] = $row['nickname'];
    $user['portrait'] = $row['portrait'];
    echo $_GET['callback']."(".json_encode($user).")";
  }

?>