2012-01-30 23 views
6

के माध्यम से एक्सएमएल फ़ाइल लोड नहीं करेगा I वेबवर्कर को मेरे मुख्य पृष्ठ के किनारे एक ही डोमेन से एक्सएमएल फ़ाइल लोड करने के लिए स्ट्रक्चरिंग कर रहा हूं, किसी भी मदद की सराहना की जाएगी।जावास्क्रिप्ट वेबवर्कर XMLHttpRequest

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 के माध्यम से चल रहा है, FireBug मुझे है अशक्त

postMessage देता

hr.responseXML मिल (। xhr.responseXML.getElementsByTagName ("मान") लंबाई);

फ़ायरबग कंसोल में, प्राप्त अनुरोध

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

तो प्रतिक्रिया सही है, लेकिन मैं समझ नहीं जहां यह गलत हो रहा है साथ जवाब दिया। तो मैं कार्यकर्ता आउटपुट

एक मूल्य एक और मूल्य एक तिहाई मूल्य

कौन सा सही है responseText को responseXML बदल! स्क्रिप्ट को इसे XML दस्तावेज़ के रूप में क्यों नहीं खोलेंगे?

अद्यतन

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

जब setRequestHeader & overrideMimeType बदल गया है, onreadystatechange कभी नहीं आग, कोई फर्क नहीं पड़ता अगर स्थिति और readyState वहाँ हैं या नहीं, यह नहीं चलेगा। अगर मैं पूरी तरह से पहले से ही हटा देता हूं और केवल xhr.responseXML चलाता हूं, तो मुझे फिर से शून्य त्रुटि मिलती है।

मुझे अभी भी कंसोल में प्रतिक्रिया के रूप में सही एक्सएमएल मिलता है, क्या यह httprequest समस्या के बजाय वेबवर्कर मुद्दा है? यहाँ हताश हो रही है :)

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

उत्तर

-2

तुम सिर्फ सर्वर साइड पर text/xml लिए सामग्री प्रकार हेडर सेट करना होगा। responseXML शून्य है यदि आप जिस दस्तावेज़ का अनुरोध कर रहे हैं वह एक्सएमएल नहीं है। विशेष रूप से, सामग्री-प्रकार text/html, text/xml, application/xml, या +xml में समाप्त होने वाला कुछ होना चाहिए। the spec देखें।

इसके अलावा, देखें: responseXML is null और responseXML always null

और एक साइड नोट: चूंकि वेब कर्मचारी स्वाभाविक रूप से असीमित हैं, इसलिए आपको अपने open कॉल में एसिंक ध्वज को सही करने की आवश्यकता नहीं है।

+0

यह अनुरोध करने के बाद भी काम नहीं करता है और मैंने माइम प्रकार को ओवरराइड करने का प्रयास किया है, प्रतिक्रियाXML अभी भी शून्य है। यह वेबवर्कर के साथ एक मुद्दा प्रतीत होता है क्योंकि स्क्रिप्ट मुख्य दस्तावेज़ में ठीक काम करती है। – axlOnline

+0

मुझे एक ही समस्या है; प्रतिक्रिया स्क्रिप्ट में पाठ/xml पर माइम प्रकार सेट किया गया है, xmlhttprequest readystate 4 है और प्रतिक्रिया टाइप खाली है और प्रतिक्रियाXML शून्य है। निराशा होती। मैं जिस वर्कअराउंड का उपयोग कर रहा हूं वह एक्सएमएल को PHP में JSON में अनुवाद कर रहा है, और इसे वापस कर रहा है, और JSON.parse का उपयोग कर, जो धन्यवाद करता है। दूसरी ओर –

3

मानक के मुताबिक, वेब श्रमिकों को किसी भी प्रकार के डोम मैनिपुलेशन तक पहुंच नहीं हो सकती है।

डीओएम एपीआई (नोड ऑब्जेक्ट्स, दस्तावेज़ ऑब्जेक्ट्स इत्यादि) इस विनिर्देश के इस संस्करण में श्रमिकों के लिए उपलब्ध नहीं हैं।

प्रतिक्रियाXML और चैनल गुण हमेशा AJAX अनुरोध से शून्य होते हैं क्योंकि XML की पार्सिंग एक डोम एपीआई है। अनुरोध और प्रतिक्रिया शीर्षकों से कोई फर्क नहीं पड़ता कि requestXML प्राप्त करने का कोई तरीका नहीं होगा जबतक कि आप इसे मैन्युअल रूप से पार्स नहीं करते।

+1

JSON.parse उपलब्ध है। यह आपको सर्वर पक्ष पर एक्सएमएल को जेएसओएन में बदलने की अनुमति देगा क्योंकि @Jon_Fournier ने कहा – georgephillips

0

एक ही समस्या थी। स्पष्ट रूप से वेबवर्कर्स में एक्सएमएल पार्सिंग संभव नहीं है। मैंने वेब कार्यकर्ता पर एक्सएमएल को पार्स करने के लिए 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 
} 

इसका कुछ खास नहीं है लेकिन आपको ऐसा करने का विचार मिलता है।