我有2個JS文件,其中一個包含一個名爲「form.js」的多個文件之間共享的泛型函數列表,另一個特定於我的CMS上的某個頁面, blog.form.js」。Ajax響應返回,說明它的屬性值未定義
內部 「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)」也會返回undefined。
XMLHTTPRequest對象已成功返回。那麼,爲什麼response.link聲明它的值是未定義的呢?
任何幫助,非常感謝。
這是一種享受!非常感謝你的提示+啓示。 – Bredcrumbs