2012-07-23 114 views
1

我想通过为每种语言创建一个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来排除故障。 这是控制台的屏幕截图。 console

因此,你看到由于某种原因,请求失败..任何想法为什么?

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); 
     } 
}); 
+0

在'success'回调中移动'console.log'调用。目前,它在异步调用返回之前运行。 – 2012-07-23 13:20:10

+0

在同一目录中是英文版的。也可以尝试在xml的顶部添加<?xml version =“1.0”?> – 2012-07-23 13:21:24

+0

@JamesAllardice无关紧要,从第一个打印语句中可以清楚地看到它在错误callaback中被调用。 – 2012-07-23 13:21:54

回答

0

我想补充

<?xml version="1.0" encoding="utf-8"?> 

到文档的顶部。我有一种感觉,因为你告诉它你的返回类型将是xml它期望这种错误,当它没有找到它时。

也不应该您的响应类型是"application/xml"而不仅仅是"xml"

此外,您将异步设置为false,这意味着您要求进行同步调用。只是柜面你想知道

编辑:我已经编辑自application/xml应该被用来代替text/xml

您还需要有一个根节点

<applicationCalls> 
    <text id="call">Caller</text> 
    <text id="chat">Chatter</text> 
</applicationCalls> 

你可以打电话给你的根节点不管你就像,试着让它变得有意义。

+0

我已经尝试了这些更改,并且也没有将async设置为false,并且仍然得到相同的响应。 – 2012-07-23 13:29:38

+0

我添加了一个根节点 <文本ID =“呼”>来电 <文本ID =“聊天”>颤振 但仍然得到同样的错误,如果我通过XML验证运行它,我得到无法找到元素'english'的声明。 – 2012-07-23 15:13:38

+0

它看起来对我来说是合法的,除非我错过了非常明显的东西,它可能无效的唯一原因是它是在检查模式,但我不明白为什么它会出现在这种情况下。 – 2012-07-23 15:24:34