使用javascript和jquery进行回调
我更像是一名java开发人员,在javascript回调方面遇到了困难。我想知道这里是否有专家能帮助我摆脱这段代码的困扰 我试图从数据库中提取我们的位置并填充到一个数组中。在第一次加载时,我试图刷新所有位置,但在控制执行流和加载值时遇到了问题。下面是代码,我在最后输入了输出 JQUERY代码:使用javascript和jquery进行回调,javascript,php,jquery,callback,Javascript,Php,Jquery,Callback,我更像是一名java开发人员,在javascript回调方面遇到了困难。我想知道这里是否有专家能帮助我摆脱这段代码的困扰 我试图从数据库中提取我们的位置并填充到一个数组中。在第一次加载时,我试图刷新所有位置,但在控制执行流和加载值时遇到了问题。下面是代码,我在最后输入了输出 JQUERY代码: // load all locations on first load. refreshLocations(); $("#locInput
// load all locations on first load.
refreshLocations();
$("#locInput").autocomplete({source: locationData});
}); // end of document.ready
// function to refresh all locations.
function refreshLocations() {
getLocationArray(function(){
console.log("firing after getting location array");
});
}
// function to get the required array of locations.
function getLocationArray() {
getJsonValues("GET", "getLocalityData.php", "", getLocalityFromJson);
}
// function to pick up localities from json.
function getLocalityFromJson(json){
if (!json) {
console.log("====> JSON IS NOT DEFINED !! <====");
return;
} else {
console.log("json is defined so processing...");
var i = 0;
$.each(json.listinginfo, function() {
var loc = json.listinginfo[i].locality;
locationArray[i] = loc;
console.log("added location ->" + locationArray[i]);
i++;
});
}
//return locationArray;
}
// function to get raw json from db.
function getJsonValues(type, url, query, getLocalityFromJson) {
var json;
// if the previous request is still pending abort.
if (req !== null)
req.abort();
var searchString = "";
if (query !== "") {
searchString = "searchStr" + query;
}
console.log("searchString : (" + query + ")");
req = $.ajax({
type: type,
url: url,
data: searchString,
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(result) {
json = JSON.parse(result);
console.log("========start of json
return============");
console.log(JSON.stringify(json));
console.log("========end of json
return============");
//return json;
}
});
getLocalityFromJson(json);
return json;
}
searchString : () (18:25:36:473)
at locality1.php:74
====> JSON IS NOT DEFINED !! <==== (18:25:36:518)
at locality1.php:48
========start of json return============ (18:25:37:606)
at locality1.php:83
{"listinginfo":[{"listing":"1","locality":"birmingham"},
{"listing":"2","locality":"oxford"}]} (18:25:37:624)
at locality1.php:84
========end of json return============ (18:25:37:642)
at locality1.php:85
>
//第一次加载时加载所有位置。
刷新位置();
$(“#locInput”).autocomplete({source:locationData});
}); // 文件结束。准备好了吗
//函数刷新所有位置。
函数刷新位置(){
getLocationArray(函数(){
log(“获取位置数组后触发”);
});
}
//函数获取所需的位置数组。
函数getLocationArray(){
getJsonValues(“GET”、“getLocalityData.php”、“getLocalityFromJson”);
}
//函数从json中提取位置。
函数getLocalityFromJson(json){
如果(!json){
log(“=>未定义JSON!!”+locationArray[i]);
i++;
});
}
//返回定位阵列;
}
//函数从数据库中获取原始json。
函数getJsonValues(类型、url、查询、getLocalityFromJson){
var-json;
//如果上一个请求仍处于挂起状态,则中止。
如果(请求!==null)
请求中止();
var searchString=“”;
如果(查询!==“”){
searchString=“searchStr”+查询;
}
log(“searchString:(“+query+”));
req=$.ajax({
类型:类型,
url:url,
数据:searchString,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“文本”,
成功:功能(结果){
json=json.parse(结果);
console.log(“=============json的开始
返回===========”;
log(JSON.stringify(JSON));
console.log(“=============json的结尾
返回===========”;
//返回json;
}
});
getLocalityFromJson(json);
返回json;
}
上述代码的输出如下:
// load all locations on first load.
refreshLocations();
$("#locInput").autocomplete({source: locationData});
}); // end of document.ready
// function to refresh all locations.
function refreshLocations() {
getLocationArray(function(){
console.log("firing after getting location array");
});
}
// function to get the required array of locations.
function getLocationArray() {
getJsonValues("GET", "getLocalityData.php", "", getLocalityFromJson);
}
// function to pick up localities from json.
function getLocalityFromJson(json){
if (!json) {
console.log("====> JSON IS NOT DEFINED !! <====");
return;
} else {
console.log("json is defined so processing...");
var i = 0;
$.each(json.listinginfo, function() {
var loc = json.listinginfo[i].locality;
locationArray[i] = loc;
console.log("added location ->" + locationArray[i]);
i++;
});
}
//return locationArray;
}
// function to get raw json from db.
function getJsonValues(type, url, query, getLocalityFromJson) {
var json;
// if the previous request is still pending abort.
if (req !== null)
req.abort();
var searchString = "";
if (query !== "") {
searchString = "searchStr" + query;
}
console.log("searchString : (" + query + ")");
req = $.ajax({
type: type,
url: url,
data: searchString,
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(result) {
json = JSON.parse(result);
console.log("========start of json
return============");
console.log(JSON.stringify(json));
console.log("========end of json
return============");
//return json;
}
});
getLocalityFromJson(json);
return json;
}
searchString : () (18:25:36:473)
at locality1.php:74
====> JSON IS NOT DEFINED !! <==== (18:25:36:518)
at locality1.php:48
========start of json return============ (18:25:37:606)
at locality1.php:83
{"listinginfo":[{"listing":"1","locality":"birmingham"},
{"listing":"2","locality":"oxford"}]} (18:25:37:624)
at locality1.php:84
========end of json return============ (18:25:37:642)
at locality1.php:85
>
searchString:()(18:25:36:473)
在locality1.php:74
=>未定义JSON!!
非常感谢您的帮助。调用
getLocalityFromJson(json)代码>在成功回调中
function getJsonValues(type, url, query, getLocalityFromJson) {
var json;
// if the previous request is still pending abort.
if (req !== null)
req.abort();
var searchString = "";
if (query !== "") {
searchString = "searchStr" + query;
}
console.log("searchString : (" + query + ")");
req = $.ajax({
type: type,
url: url,
data: searchString,
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(result) {
json = JSON.parse(result);
console.log("========start of json return============");
console.log(JSON.stringify(json));
console.log("========end of json return============");
//return json;
getLocalityFromJson(json);
}
});
}
您需要调用getLocalityFromJson(json)并在ajax成功函数中返回json。Ajax请求是异步的,不能保证在到达getLocalityFromJson(json)行时请求会完成;返回(json);当前位置。jquery ajax调用的回调函数完成、失败、成功等。。
请求成功后调用Success,
如果出现错误500、404或w/e,则调用Failure。
Complete总是在ajax调用之后调用
如果您想让您的代码像java中一样遵循顺序,请在ajax调用中抛出async:false
。。但我不推荐这种方法,因为它违背了使用这种方法的目的,也会锁定你的浏览器
在继续之前,您应该确保正在等待请求完成-因此,请在请求完成获取数据后,将要运行的代码放入success函数中。我想您需要记住Ajax是异步运行的,因此,您需要按照此线程执行刷新。完成了此操作,但刷新位置中的函数不会打印控制台日志。所有回调的语法都好吗?或者需要修改?另外,请告诉我在netbeans中调试js时可以使用的最佳调试方法是什么?因为从未调用refreshLocations
中的函数。