无法从api

时间:2015-10-17 14:19:24

标签: javascript php jquery json api

我有自己的用PHP编写的api。基本上我有一个下拉列表,用输入的关键字调用我的api。 示例输入关键字be = test。因此,使用关键字参数对带有GET请求的api / search /进行ajax调用。

api搜索功能。

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响应正确 - 让示例keyword = test .. 响应是=

[{"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"}]

我的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]]);
            });


        }
    });

}

});

我想在Jquery的回调中得到api响应。

2 个答案:

答案 0 :(得分:1)

返回data不是对象的字符串。你需要先解析它:

使用它:

data = JSON.parse(data);

或者您也可以使用jquery来解析json:

$.parseJSON( data);

同时使用success代替complete

答案 1 :(得分:0)

问题解决了: 我用Success完成了替换。谢谢@madforstrength

并添加了$ .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]);
        }