在此基礎上SO answer,我這樣做:jQuery的AJAX調用不接受響應數據
$(document).ready(function() {
$('#stateid').change(function() {
$.ajax({
url: "/admin/state_teamlist.php",
data: {
stateid: $("#stateid").val()
},
dataType: "json",
success: function (json) {
$('#teamid').empty().each(json, function (value, key) {
$('#teamid').append($("<option></option>")
.attr("value", value).text(key));
});
}
});
});
});
返回的數據是有效的JSON(從PHP的json_encode):
{"16734":"Anchorage Christian","12241":"Barrow","1060":"Bartlett","1064":"Chugiak","5802":"Colony","1061":"Dimond","1058":"Eagle River","1063":"East","3943":"Eielson","7104":"Homer","11597":"Houston","5568":"Juneau-Douglas","10229":"Kenai Central","14260":"Ketchikan","9444":"Kodiak","8443":"Lathrop","12152":"Monroe Catholic","7554":"Nikiski","4082":"North Pole","1065":"Palmer","1057":"Service","14370":"Seward","12271":"Sitka","11780":"Skyview","3308":"Soldotna","844":"South","9919":"Thunder Mountain","13803":"Valdez","9766":"Wasilla","1059":"West","1062":"West Valley"}
但我出現此錯誤:
未捕獲TypeError:對象#沒有方法「應用」
中的jquery.js的錯誤行是:
if (callback.apply(obj[ i++ ], args) === false) {
解決這個問題的完整的部分是:
// args is for internal usage only
each: function(obj, callback, args) {
var name,
i = 0,
length = obj.length,
isObj = length === undefined || jQuery.isFunction(obj);
if (args) {
if (isObj) {
for (name in obj) {
if (callback.apply(obj[ name ], args) === false) {
break;
}
}
} else {
for (; i < length;) {
if (callback.apply(obj[ i++ ], args) === false) {
Uncaught TypeError: Object #<Object> has no method 'apply'
break;
}
}
}
// A special, fast, case for the most common use of each
}
的teamid選擇列表清空正確的,但不與JSON數據重新填充。如果需要,我可以發佈HTML,但我不認爲這是一個問題。
我已經經歷過每個關於該主題的答案,但沒有一個解決方案適合我的問題。爲什麼我得到這個錯誤?
我試過jquery.min.js,不同版本的jQuery,鮑勃迪倫的$('teamid')
到$el
像引用,以便回答,.post的使用和不用彷徨,而不是阿賈克斯和似乎沒有任何解決它。請幫忙。
做一次一個工作。我的想法是將這兩種行爲都應用於同一個ID,那麼爲什麼不把它們連接起來呢?我想jQuery的作品不同於我的思維過程。我選擇了這個答案,因爲他們都說了同樣的事情,這是第一個。 – MECU