获得响应来提供错误的Json格式。如何让它以所需的格式回复?

时间:2017-07-04 14:25:05

标签: php angular

我正在尝试在我的应用程序中创建搜索模块,但我的数据库的响应采用以下格式,我收到错误

ERROR SyntaxError: Unexpected token s in JSON at position 0

sa[{"id":"4","worklocation":"Hyderabad","firstname":"Sanjay","lastname":"Tutuki","gender":"Male","aadharno":"0","panno":"2147483647","employeetype":"asdasd","dateofbirth":"2017-07-12","city":"Mumbai"}]

如果没有我搜索过的字词,我如何回复?或者如何在没有错误的情况下获取组件中的值。

这是我的代码:

service.ts:

search(terms: Observable<string>) {
    return terms.debounceTime(400)
      .distinctUntilChanged()
      .switchMap(term => this.searchEntries(term));
  }
            searchEntries(term) {
                const baseUrl="http://localhost/Angular/search.php?search="
    return this.http
        .get(baseUrl + term)
        .map(res => res.json());
  }

组件:

export class EmployeeDirectory { 
    data : Data[];
    searchTerm$ = new Subject<string>();

    constructor(private dataservice : DataService){

        this.dataservice.search(this.searchTerm$)
      .subscribe(data => {
        this.data = data.data;
        console.log(data);

      });
  }

  search(searchTerm$){
    this.dataservice.search(this.searchTerm$)
      .subscribe(data => {
        this.data = data.data;
        console.log(data);
      });
  }
    }

模板:

<div class="col-md-6 col-sm-12 col-xs-12 ">
   <form method="GET" name="form" autocomplete="off">
    <div class="col-lg-9">
    <div class="input-group">
      <input type="text" class="form-control" (keyup)="searchTerm$.next($event.target.value)"  placeholder="Search for...">
      <span class="input-group-btn">
        <button class="btn btn-secondary" type="button" (click)="search(this.searchTerm$)" ><i class="glyphicon glyphicon-search"></i></button>
      </span>
    </div>
  </div>
  </form>
</div>


   <div *ngIf="data">
      <table *ngFor="let employee of data">
        <tr><td>First Name: </td><td>{{employee.firstname}}</td></tr>
      </table>

PHP文件:

<?php 
header("Allow-Access-Header-Origin: *");
$servername ="localhost";
$username ="root";
$password="sandeepchetikam";
$dbase = "mydb";

$conn = mysqli_connect($servername,$username,$password,$dbase);
if(!$conn){
    echo "Error" .mysqli_error($conn);
}
$name =$_GET['search'];
print_r($name);
$sql = "SELECT * FROM newdb WHERE firstname LIKE '%{$name}%' ";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result))
{
    $rows[] = $row;
}
echo json_encode($rows);
?>

此功能仅响应第一次搜索键,并且不适用于其他搜索项。谁能帮我这个?

2 个答案:

答案 0 :(得分:2)

print_r($name);

看起来有点可疑

答案 1 :(得分:1)

从您的代码中删除print_r($name);。首先打印数据,这也是响应数据,最后一个echo json数据也响应数据,因此您的正确响应数据会发生变化。如果您在文件中编写html代码或回显或打印数据,那么这将更为重要,然后它将作为响应发送。

所以你的总代码将是

<?php 
header("Allow-Access-Header-Origin: *");
$servername ="localhost";
$username ="root";
$password="sandeepchetikam";
$dbase = "mydb";

$conn = mysqli_connect($servername,$username,$password,$dbase);
if(!$conn){
    echo "Error" .mysqli_error($conn);
}
$name =$_GET['search'];
$sql = "SELECT * FROM newdb WHERE firstname LIKE '%{$name}%' ";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result))
{
    $rows[] = $row;
}
echo json_encode($rows);
?>

希望它会帮助你