我想通过为每种语言创建一个xml文件来本地化我的字符串,然后使用ajax得到像这样。Jquery ajax奇怪的错误
var text = new Object();
$.ajax({
async: false,
type: 'GET',
url: 'english.xml',
dataType: "xml",
error: function(xhr, status, error) {
console.log("Lets see the error...");
console.log(xhr.responseText);
},
success: function(xml){
$(xml).find('text').each(function(){
text[$(this).attr('id')] = $(this).text();
});
}
});
console.log("Lets see the object...");
console.log(text);
我添加了一些console.logs来排除故障。 这是控制台的屏幕截图。
因此,你看到由于某种原因,请求失败..任何想法为什么?
english.xml只包含:
<text id="call">Caller</text>
<text id="chat">Chatter</text>
更新: 更改数据类型为文本,现在越来越成功响应,但“文本”对象没有被更新?
var text = new Object();
$.ajax({
type: 'GET',
url: 'english.xml',
dataType: "text",
error: function(xhr, status, error) {
console.log(xhr);
console.log(xhr.responseText);
console.log(status);
console.log(error);
},
success: function(xml){
$(xml).find('text').each(function(){
text[$(this).attr('id')] = $(this).text();
});
console.log(xml);
console.log(text);
}
});
在'success'回调中移动'console.log'调用。目前,它在异步调用返回之前运行。 – 2012-07-23 13:20:10
在同一目录中是英文版的。也可以尝试在xml的顶部添加<?xml version =“1.0”?> – 2012-07-23 13:21:24
@JamesAllardice无关紧要,从第一个打印语句中可以清楚地看到它在错误callaback中被调用。 – 2012-07-23 13:21:54