我想在我的应用程序创建一个搜索模块,但是从我的数据库的响应是以下格式并且我得到一个错误从服务器获取响应的Json格式错误。如何使其以所需格式响应?
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) {
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();
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);
});
}
}
模板:
First Name: | {{employee.firstname}} |
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);
?>
而且该函数仅对第一个搜索键作出响应,并且它不适用于其他搜索项。谁能帮我这个?
2017-07-04
Prakash
+1
你没有删除'的print_r($名)'当你正在测试这个权利? –
+0
是的,这是问题。感谢大家 。有人可以帮助我吗?我如何使搜索按钮作为搜索而不是关键?如何发送searchTerm $的按钮? –
+0
我很确定你可以谷歌。 –