AJAX"回调是未定义"错误 [英] ajax "callback is undefined" error
问题描述
目前我正在试图返回一个数据库调用,以填补一个下拉框。当我去循环通过返回的列表,但是,我得到一个回调是不确定的错误。我试过这个code两种方式,也不是工作。
I am currently trying to return a database call to fill a dropdown box. When I go to cycle through the list returned, however, I am getting a "callback is undefined" error. I've tried this code two ways, and neither is working.
我已经试过:
$('#Vehicle_KovId_value').change(function () {
var kovID = $(this).val();
var drop2 = $('#Vehicle_BodyStyle_value');
if (kovID != null && kovID != '') {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option('Please Select One', '-1');
$.ajax({
type: "GET",
url: '/Ajax/Index',
async: false,
data: { KovID: kovID },
contentType: "application/object; charset=utf-8",
success: function (record) {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option("Please Select One", "-1");
$.each(function (index, item) {
drop2.get(0).options[drop2.get(0).options.length] = new Option(item.Display, item.Value);
});
},
error: function () {
$('#Vehicle_BodyStyle_value').get(0).options.length = 0;
$('#Vehicle_BodyStyle_value').get(0).options[0] = new Option("Error!", "-1");
alert("Failed to load styles");
}
});
}
});
我也试过:
$('#Vehicle_KovId_value').change(function () {
var kovID = $(this).val();
var drop2 = $('#Vehicle_BodyStyle_value');
if (kovID != null && kovID != '') {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option('Please Select One', '-1');
$.ajax({
type: "GET",
url: '/Ajax/Index',
async: false,
data: { KovID: kovID },
contentType: "application/object; charset=utf-8",
success: function (record) {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option("Please Select One", "-1");
fillBStyles(record);
// $.each(function (index, item) {
// drop2.get(0).options[drop2.get(0).options.length] = new Option(item.Display, item.Value);
// });
},
error: function () {
$('#Vehicle_BodyStyle_value').get(0).options.length = 0;
$('#Vehicle_BodyStyle_value').get(0).options[0] = new Option("Error!", "-1");
alert("Failed to load styles");
}
});
}
});
function fillBStyles(r) {
var drop2 = $('#Vehicle_BodyStyle_value');
$.each(function (index, item) {
drop2.get(0).options[drop2.get(0).options.length] = new Option(item.Display, item.Value);
});
}
其中
这两个给我的错误:
Both of which give me the error:
类型错误:回调未定义
该数据对象被返回,记录
,就是我要拉两件从数据库对象的列表。
The data object being returned, record
, is a list of database objects that I have to pull two pieces from.
我怎样才能解决这个回调的错误,使我可以用我的数据,我的功能?
How can I fix this "callback" error so that I can use my data in my function?
推荐答案
一个回调将被执行后的predecessor完成它的操作,并返回一个值的函数。在这种情况下,你有两个回调,成功与失败。
A callback is the function to be executed immediately after its predecessor completes it's operation and returns a value. In this case you have two callbacks, success and failure.
函数声明没有内部块允许(的if / else /闭包),这意味着你的回调函数(声明的AJAX闭包)函数(记录){}
不能放在那里是因为它是在你的如果(kovID = NULL和放大器;!&安培;!kovID =''){}
关闭
Function declarations are not allowed inside blocks (if/else/for closures) which means your callback function (which you declare inside your ajax closure) function (record) { }
cannot be placed where it is since it is inside your if (kovID != null && kovID != '') { }
closure.
幸运的是有一个简单的办法:申报你的成功回调函数外的if语句(把它放在自己在全球范围内)是这样的:
Fortunately there is an easy fix: declare your success callback function outside the if statement (put it by itself in global scope) like this:
function ajaxSuccess(record) {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option("Please Select One", "-1");
$.each(function (index, item) {
drop2.get(0).options[drop2.get(0).options.length] = new Option(item.Display, item.Value);
});
}
// ...do other javascript stuff
和调用成功,函数是这样的:
and call the success function like this:
$.ajax({
type: "GET",
url: '/Ajax/Index',
async: false,
data: { KovID: kovID },
contentType: "application/object; charset=utf-8",
success: ajaxSuccess
//...blah blah blah
这篇关于AJAX"回调是未定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!