Ajax对自动完成插件的响应

时间:2015-10-23 01:00:36

标签: javascript php ajax

自动填充文本字段的代码如下:

var ColTags = [
    "USA","Canada","China"
];

我需要使用Ajax来填充脚本,但我没有JavaScript经验。

$.ajax({
    data:    parameter,
    url:     'country.php',
    type:    'post', 
    success: function (response) {

        var ColTags = [
            response
        ];

        $( "#ColTags" ).autocomplete({
            source: ColTags
        });
    }
});

country.php

$var="'uno','dos','tres','cuatro'";
echo $var;

1 个答案:

答案 0 :(得分:1)

这不是javascript的工作原理。阅读更多相关信息。有很多在线教程可以解释得比我好得多。

$.ajax({
    data:  parameter,
    url:   'country.php',
    type:  'post', 
    success:  function (response) {
        //error checking is good
        if(! (response instanceof Array))
             console.log (response);
             return false;
         }
        var colTags = response; 

        $( "#ColTags" ).autocomplete({
            source: colTags
        });

    }
});

// country.php

$var=array('uno','dos','tres','cuatro');
echo json_encode($var);