jquery - Jquery Autocomplete Load Data at once ($.ajax vs $.get/$.post)
问题描述
Somebody please help, really curious about this.
I want autocomplete only get data source only once at page load. I've tried two ways.
First, with $.ajax(GET/POST) and it was successful
$.ajax({
type: "GET",
url: "search.php",
data: {
action: 'getCoa'
},
dataType: "json",
success: function(data) {
$('#coa').autocomplete({
source: data
});
},
error: function(result) {
alert("Error");
}
});
Second way, with $.post/$.get but failed
//1st try - failed
$.post('search.php', {
action: 'getCoa'
}, function(result) {
$('#coa').autocomplete({
source: result
});
});
//2nd try - failed
var test = $.post("search.php", {
action: 'getCoa'
}, function(result) {
$('#coa').autocomplete({
source: result
});
})
.done(function(result) {
$('#coa').autocomplete({
source: result
});
})
.fail(function() {
alert("error");
})
.always(function() {
alert("finished");
});
//3rd - failed
var coaList = [];
$.get("search.php?action=getCoa", function(data, status) {
console.log(coaList);
coaList = data;
$('#coa').autocomplete({
source: result
});
});
Any idea why $.post/$.get fail ? Is there somethin wrong with my $.post/$.get ? Or whether this way can't be used in this case ?
Thnks & Brgds
解决方案
您需要在函数之后dataType
的最后一个$.post()
参数中指定 JSON callback
,这是您在第一个$.ajax()
适用于您的方法中指定的。
$.post('search.php', {
action: 'getCoa'
}, function(result) {
$('#coa').autocomplete({
source: result
});
}, 'json');
推荐阅读
- apache-flink - 每个子任务/分区是否可以有一个窗口
- php - 使用解码的 json 字符串插入新行
- python - __init__() 为参数“填充”获得了多个值
- rabbitmq - RabbitMQ 绑定键 - 最佳实践
- logging - 如何在 k8s 节点中管理系统日志/内核日志轮换策略
- autodesk-forge - 无法在 Autodesk Forge Viewer 中更改对象颜色
- python - 在 macOS Big Sur 上安装包“pyplatefit”时失败
- java - Exchange 混合 - 外部 EWS URL 引发连接超时错误
- android - 您的应用容易受到 Intent 重定向的影响
- module - 如何要求 Arduino 中的两个 BC04-B 蓝牙模块正常断开连接?