我有 2 个 JS 文件,一个包含要在多个名为“form.js”的文件之间共享的通用函数列表,另一个特定于我的 CMS 上的某个页面,名为“blog.form.js”。
在“form.js”中,我有一个通用的 JS 函数,每当我请求从数据库加载记录时,它都会发出 jQuery.ajax() 请求:
function load_record( field_id, class_name, entity_type ) {
// Send ajax request to load the record, and enable the form's state once the record's content has been received.
var response = $.ajax({
async: false,
dataType: "json",
data: {
action: "load_"+entity_type,
id: $("#"+field_id+"_list").val()
},
success: function(response) {
// Make visible the buttons to allow actions on record, such as deleting or renaming.
$("#"+field_id+"_actions").show();
// Make visible the container of all form elements related to the record.
$("#"+field_id+"_form_inputs").show();
// Must return response so the calling JS file can use the values returned to
// populate the form inputs associated with the record that's just been loaded
// with the correct values.
return response;
},
type: "post",
url: "/ajax/record/"+class_name
});
alert( response.link + " " + response + " " + response.responseText);
return response;
}
在“blog.form.js”中,当我从包含它们列表的菜单中选择要加载的数据库记录时,我调用了函数:
// Select a link for editing.
$("#links_list").live( "change", function(){
// Insert response returned from function call to load the db record into a variable.
// This is so the form inputs associated with the record loaded can be populated with the correct values.
var response = load_record('links_edit', 'blog', 'link');
alert( response.link );
$("#links_edit_url").val( response.link );
});
ajax 请求返回所需的响应。不幸的是,load_record() 中的调试警报语句“alert( response.link + " " + response + " " + response.responseText)" 返回以下内容: undefined [Object XMLHTTPRequest] {"link": "http://www .url.com”}。
因此,另一个函数中的调试警报语句“alert(response.link)”也返回未定义。
成功返回一个 XMLHTTPRequest 对象。那么,为什么 response.link 声明它的值是未定义的呢?
任何帮助深表感谢。