2012-01-30 14 views
6

私のメインページの横にある同じドメインからXMLファイルを読み込むためにWebワーカーを手伝って欲しいです。これは、メインページのスクリプトタグで実行されている場合Javascript WebworkerはXMLHttpRequestでXMLファイルをロードしません

function readXML(){ 
var xhr = new XMLHttpRequest(); //Only for FF 
xhr.open("GET","../db/pointer.xml",true); 
xhr.send(null); 
xhr.onreadystatechange = function(e){ 

if(xhr.status == 200 && xhr.readyState == 4){ 
    //Post back info to main page 
    postMessage(xhr.responseXML.getElementsByTagName("value").length); 
} 
} 

は、私は3 WebWorkerを通して実行されている、放火犯は私にヌル

のpostMessageを

hr.responseXMLをされています取得します(xhr.responseXML.getElementsByTagName( "value")。length); Firebugのコンソールで

、リクエストだから応答が正しいですが、私はそれが間違って起こっている場所を把握することはできません

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <value>A value</value> 
    <value>Another Value</value> 
    <value>A third Value</value> 
</root> 

と答えましょう。 私は労働者の出力

値別の値3番目の値

が正しいresponseTextするのresponseXMLを変更した場合は!なぜスクリプトはそれをXML文書として開きませんか?ステータスとreadyStateのがあったりでない場合は関係ありません

function readXML(){ 
var xhr = new XMLHttpRequest(); //Only for FF 
xhr.open("GET","../db/pointer.xml",false); 
xmlhttp.setRequestHeader('Content-Type', 'text/xml'); 
xhr.overrideMimeType('text/xml'); 
xhr.send(null); 
xhr.onreadystatechange = function(e){ 

if(xhr.status == 200 && xhr.readyState == 4){ 
    //Post back info to main page 
    postMessage(xhr.responseXML.getElementsByTagName("value").length); 
} 
} 

UPDATE setRequestHeader & overrideMimeTypeが変更されると、onreadystatechangeには発射したことがない、それは実行されません。私がonreadystatechangeを完全に削除してxhr.responseXMLを実行すると、再びnullエラーが発生します。

私はまだコンソールで応答として正しいXMLを取得しますが、これはhttprequestの問題ではなくウェブワーカーの問題ですか?ここで必死:)あなただけのサーバー側でtext/xmlにContent-Typeヘッダを設定する必要が

index.htmlをhttp://www.axlonline.se/worker/index.html
worker.js http://www.axlonline.se/worker/worker.js

答えて

-2

を。 responseXMLは、要求しているドキュメントがXMLでない場合はnullです。具体的には、コンテンツタイプはtext/html,text/xml,application/xml、または末尾が+xmlのいずれかである必要があります。 the specを参照してください。

responseXML is nullおよびresponseXML always nullも参照してください。

また、Webワーカーは本質的に非同期であるため、open呼び出しでasyncフラグをtrueに設定する必要はありません。

+0

requestheaderを追加してmimeタイプをオーバーライドしようとしても、responseXMLはまだnullです。 スクリプトはメイン文書でうまく動作するので、Webワーカーの問題であるようです。 – axlOnline

+0

私は同じ問題があります。応答スクリプトでMIMEタイプがtext/xmlに設定されている場合、xmlhttprequest readystateは4、responseTypeは空白、responseXMLはnullです。挫折。 私が使用していた回避策の1つは、XMLをPHPのJSONに変換して戻し、JSON.parseを使用していたことです。一方、JSON.parseは利用可能です。 –

3

標準によれば、WebワーカーはどのようなタイプのDOM操作にもアクセスできない可能性があります。

この仕様のこのバージョンでは、DOM API(ノードオブジェクト、ドキュメントオブジェクトなど)は使用できません。

XMLの解析がDOM APIであるため、responseXMLとchannelプロパティは常にajaxリクエストからnullです。要求と応答のヘッダーに関係なく、手動で解析しない限り、requestXMLを取得する方法はありません。

+1

これは、@ Jon_Fournierが述べたように、XMLをサーバー側のJSONに変更することを可能にします – georgephillips

0

同じ問題がありました。どうやらXML解析はウェブワーカーにとって不可能です。 WebワーカーでXMLを解析するためにsax.jsを使用しました。 https://github.com/isaacs/sax-js

これは基本的にパーサーです。

function xmlParser(strict){ 
    this.parser = sax.parser(strict, {lowercase:true}); 
} 

xmlParser.prototype.parseFile = function(file, callback){ 
    var _this = this; 
    $.ajax.get({ 
     cache: false, 
     url: file, 
     dataType: "xml", 
     success: function(data){ 
      var dom = _this.parseText(data.text); 
      callback(dom); 
     }, 
     error: function(data){ 
     } 
    }); 
} 

xmlParser.prototype.parseText = function(xlmText){ 
    var dom = undefined; 
    var activeNode = dom; 

    this.parser.onerror = function (e) { }; 
    this.parser.onend = function() {}; 

    this.parser.ontext = function (t) { 
     if(activeNode != undefined) 
      activeNode.Text = t; 
    }; 
    this.parser.onopentag = function (node) { 
     var node = new xmlNode(node.name, activeNode, node.attributes, dom); 
     if(dom === undefined){ 
      dom = node; 
      activeNode = node; 
     }else{ 
      activeNode.Children.push(node); 
      activeNode = node; 
     } 
    }; 
    this.parser.onclosetag = function (node) { 
     activeNode = activeNode.Parent; 
    }; 

    this.parser.write(xlmText).close(); 
    return dom; 
} 

xmlNodeは、jqueryのようなツリーの処理を可能にします。

function xmlFilterResult(){ 
    this.length = 0; 
} 

xmlFilterResult.prototype.push = function(element){ 
    this[this.length++] = element; 
} 

xmlFilterResult.prototype.attr = function(atribute){ 
    if(this.length == 0) 
     return ''; 
    return this[0].Attributes[atribute]; 
} 
xmlFilterResult.prototype.text = function(atribute){ 
    if(this.length == 0) 
     return ''; 
    return this[0].Text; 
} 

xmlFilterResult.prototype.children = function(search, result){ 
    if(result == undefined) 
     result = new xmlFilterResult(); 
    if(search == undefined){ 
     for(var i = 0; i < this.length; i++){ 
      this[i].children(search, result); 
     } 
    }else{ 
     this.find(search, true, result); 
    } 
    return result; 
} 
xmlFilterResult.prototype.find = function(search, nonrecursive, result){ 
    if(result == undefined) 
     result = new xmlFilterResult(); 
    if(search.charAt(0) == '.') 
     return this.findAttr('class', search.substring(1), nonrecursive, result); 
    else if(search.charAt(0) == '#') 
     return this.findAttr('id', search.substring(1), nonrecursive, result); 
    else 
     return this.findName(search, nonrecursive, result); 
} 
xmlFilterResult.prototype.findAttr = function(attr, value, nonrecursive, result){ 
    if(result == undefined) 
     result = new xmlFilterResult(); 
    var child; 
    for(var i = 0; i < this.length; i++){ 
     child = this[i]; 
     child.findAttr(attr, value, nonrecursive, result); 
    } 
    return result 
} 
xmlFilterResult.prototype.findName = function(name, nonrecursive, result){ 
    if(result == undefined) 
     result = new xmlFilterResult(); 
    var child; 
    for(var i = 0; i < this.length; i++){ 
     child = this[i]; 
     child.findName(name, nonrecursive, result); 
    } 
    return result 
} 
// xmlFilterResult.prototype.findID = function(id, nonrecursive){ 
    // var child, result = new xmlFilterResult(); 
    // for(var i = 0; i < this.length; i++){ 
     // child = this[i]; 
     // child.findID(id, nonrecursive, result); 
    // } 
    // return result 
// } 




function xmlNode(name, parent, atributes, root){ 
    this.Name = name; 
    this.Children = []; 
    this.Parent = parent; 
    this.Attributes = atributes; 
    this.Document = root; 
    this.Text = ''; 
} 

xmlNode.prototype.attr = function(atribute){ 
    return this.Attributes[atribute]; 
} 
xmlNode.prototype.text = function(atribute){ 
    return this.Text; 
} 

xmlNode.prototype.children = function(search, result){ 
    if(result == undefined) 
     result = new xmlFilterResult(); 
    if(search == undefined){ 
     for(i in this.Children) 
      result.push(this.Children[i]); 
    }else{ 
     return this.find(search, true, result); 
    } 
    return result; 
} 
xmlNode.prototype.find = function(search, nonrecursive, result){ 
    if(search.charAt(0) == '.') 
     return this.findAttr('class', search.substring(1), nonrecursive, result); 
    else if(search.charAt(0) == '#') 
     return this.findAttr('id', search.substring(1), nonrecursive, result); 
    else 
     return this.findName(search, nonrecursive, result); 
} 
xmlNode.prototype.findAttr = function(attr, value, nonrecursive, result){ 
    var child, i; 
    if(result == undefined) 
     result = new xmlFilterResult(); 
    for(i in this.Children){ 
     child = this.Children[i]; 
     if(child.Attributes[attr] == value) 
      result.push(child); 
     if(!nonrecursive) 
      child.findAttr(attr, value, nonrecursive, result); 
    } 
    return result 
} 
xmlNode.prototype.findName = function(name, nonrecursive, result){ 
    var child, i; 
    if(result == undefined) 
     result = new xmlFilterResult(); 
    for(i in this.Children){ 
     child = this.Children[i]; 
     if(child.Name == name){ 
      result.push(child); 
     } 
     if(!nonrecursive) 
      child.findName(name, nonrecursive, result); 
    } 
    return result 
} 

その何も特別なものではありませんが、あなたはそれを行うという考えを得ています。

関連する問題