1

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.

Shubham Gupta
  • 81
  • 1
  • 7
  • I am only getting OK as a response via callback. – Shubham Gupta Oct 17 '15 at 14:20
  • hae you tried using success instead of complete? – Muhammad Bilal Oct 17 '15 at 14:31
  • Let's go ahead and get the standard [mysql_* functions are deprecated, switch to PDO or mysqli_](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) out of the way. (I would go with PDO. Read atleast the first 2 answers on that link. Very very helpful.) Also, [be wary of sql injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –  Oct 17 '15 at 14:32
  • @madforstrength yes by using success i am getting [object Object] as response. – Shubham Gupta Oct 17 '15 at 14:34
  • 1
    @Terminus yes i know mysql* functions are deprecated, actually this my earlier project, now i am doing changes in it and soon will shift from mysql to mysqli. – Shubham Gupta Oct 17 '15 at 14:35
  • Another thing is, you shouldn't need to query the same table 3 times. You only the query with `LIKE '%$keyword%'` per table. And once you so that, you can start using UNION ALL (because, presumably, there are not duplicates in the different tables) which should speed up the query –  Oct 17 '15 at 14:38
  • @ShubhamGupta ah, my apologies then. Happy programming! –  Oct 17 '15 at 14:39
  • @ShubhamGupta you can see complete object by doing `console.log(data)` – Muhammad Bilal Oct 17 '15 at 14:42

2 Answers2

1

Returned data is not object its string. You need to parse it first:

use this:

data = JSON.parse(data);

or you can use jquery to parse json as well:

$.parseJSON( data);

Also Use success instead of complete

Muhammad Bilal
  • 2,106
  • 1
  • 15
  • 24
0

Problem solved: I replaced complete with Success. Thanks @madforstrength

and added $.each

success: function(data) {
            var res = new Array();
            var counter = 0;
            $.each(data, function(index, element) {
                //callback([element.name]);
                res[counter] = element.name;
                counter++;
            });
            callback([res]);
        }
Shubham Gupta
  • 81
  • 1
  • 7