2011-02-01 71 views
0
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 

<script> 
$(document).ready(function(){ 

    $.ajax({ 
     type: "POST", 
     url: "http://localhost:8888/index.php/welcome/get_client/", 
     dataType: "json", 
     data: "{}", 
     success: function(data) { 
      var datafromServer = data.split(","); 
      ("#search_client").autocomplete({ 
       source: datafromServer 
      }); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown) { 
     alert(textStatus); 
     } 
    }); 

}); 
</script> 

以上是我的jQuery代码。我真的从教程中得到它,但对于我的生活,我似乎无法得到它的工作,现在我想知道如果这是我的PHP PHP结果。jQuery自动完成:没有得到任何结果。有任何想法吗?

我的PHP是这样的:

function get_client() 
{ 
    $this->db->select('name')->from('clients'); 
    $query = $this->db->get(); 

    header('Cache-Control: no-cache, must-revalidate'); 
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
    header('Content-type: application/json'); 
    echo json_encode($query->result()); 
} 

当我赞同它,它看起来像这样:

[{"name":"Testing"},{"name":"Testing1"},{"name":"test11"},{"name":"test4"},{"name":"Testing21"},{"name":"Just Testing"},{"name":"testy"}] 

我得到了以下JavaScript错误:

TypeError: Result of expression 'data.split' [undefined] is not a function. 

不确定要做什么。

+0

你设置的内容类型应用程序/ json在您的PHP HTTP响应头中? – padis 2011-02-01 22:20:14

+0

你尝试过直接点击网址吗? – Victor 2011-02-01 22:20:42

回答

0

这段时间,这个问题很简单:我离开签署此美元:

("#search_client").autocomplete({ 
1

该数据结果没有属性d因此data.d.split(",");将为空。 而不是尝试:

$.ajax({ 
     type: "POST", 
     url: "http://localhost:8888/index.php/welcome/get_client/", 
     dataType: "json", 
     data: "{}", 
     success: function(data) {     
      ("#search_client").autocomplete({ 
       source: data 
      }); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown) { 
      alert(textStatus); 
     } 
    }); 

编辑:居然没有任何需要分割数据....

0

您可能要检查的MIME类型的反应也一样,它应该是文本/ json或application/json我相信。

编辑我倾向于应用/ JSON,见the right JSON content-type