I have my own api written in php. Basically i have a dropdown which call my api with the entered keyword. Example entered keyword be = test. Hence ajax call is made to api/search/ with GET request with keyword parameter.
api search function.
public function search() {
if($this->get_request() != "GET") {
$this->response("Wrong Request");
}
$keyword = $this->request['keyword'];
if(!empty($keyword)) {
$query = "SELECT id,name FROM salon WHERE name LIKE '$keyword% %'
UNION
SELECT id,name FROM salon WHERE name LIKE '% $keyword%'
UNION
SELECT id,name FROM salon WHERE name LIKE '$keyword%'
UNION
SELECT id,subcat FROM service WHERE subcat LIKE '$keyword% %'
UNION
SELECT id,subcat FROM service WHERE subcat LIKE '% $keyword%'
UNION
SELECT id,subcat FROM service WHERE subcat LIKE '$keyword%'
UNION
SELECT staff_id,staff_name FROM staff WHERE staff_name LIKE '$keyword% %'
UNION
SELECT staff_id,staff_name FROM staff WHERE staff_name LIKE '% $keyword%'
UNION
SELECT staff_id,staff_name FROM staff WHERE staff_name LIKE '$keyword%'";
$result = mysql_query($query);
$sum_result = array();
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
$sum_result[] = $row;
}
$this->response(json_encode($sum_result));
} else {
$this->response("No Content Found");
}
} else {
$this->response("Keyword Empty");
}
}
API response is getting right - let example keyword = test .. response is =
[{"id":"354118C3-EA70-454D-AE19-46DBD7E3B5D6","name":"test multi"},{"id":"955B6DF0-0666-40A1-8906-23B4348F449B","name":"test ses"},{"id":"A7598E0C-D80B-4F72-9E6C-9DD5620F68D3","name":"Test 3"},{"id":"C33AC5F0-21A2-484E-8A29-DC6D183EB067","name":"Test 1"},{"id":"EC52F80A-C9CE-4642-963F-0BE7D96F5E36","name":"Test 2"},{"id":"F2333012-EE1A-42DA-A892-F9D7F986E287","name":"Test 4"}]
My JQuery:
$('.salon-service').autocompleteInput(function(text,callback) {
if (!text) {
callback(['January','February','March','April','May','June','July','August','September','October','November','December']);
} else {
$.ajax({
url: 'api/search/',
type: 'GET',
dataType: 'json',
asyn: false,
data: { keyword: text },
complete: function(data) {
var convert = Object.keys(data).map(function(k) {
callback([data[k]]);
});
}
});
}
});
I want to get that api response in callback of Jquery.