2013-08-02 122 views
0

我正在尝试使用XMLHttpRequest将XML数据提交给外部API,该API以XML的形式返回响应。XMLHttpRequest返回无响应和XML错误

我写了下面的代码,但它没有得到任何回应。虽然我得到200 http响应。在Firebug中,我得到的响应标签为空,而XML标签向我显示此错误“XML解析错误:没有找到元素位置:moz-nullprincipal”

任何人都可以请指出问题,我正在我的本地机器上运行它。以下是我的完整代码。

<!DOCTYPE html> 
<html> 
<head> 
<script> 
function loadXMLDoc() 
{ 
var xmlhttp; 

var data; 
data='<?xml version="1.0" encoding="utf-8" ?>'; 
data=data+'<LeadImportDatagram>'; 
    data=data+'<CompanyKey>302455</CompanyKey>'; 
    data=data+'<LeadInfo>;' 
     data=data+'<Category>Gutters Drains</Category>'; 
     data=data+'<Subcategory>Broken Gutter Repair</Subcategory>'; 
     data=data+'<OfferName>First month free with one year contract</OfferName>'; 
     data=data+'<Contact>Graham Carpenter</Contact>'; 
     data=data+'<Company>Union Boxing Company</Company>'; 
     data=data+'<Address>34 Railroad Rd.</Address>'; 
     data=data+'<Address2></Address2>'; 
     data=data+'<City>Newark</City>'; 
     data=data+'<State>DE</State>'; 
     data=data+'<Zip>34852</Zip>'; 
     data=data+'<Country>United States</Country>'; 
     data=data+'<Phone>800-842-2345</Phone>'; 
     data=data+'<PhoneExt>113</PhoneExt>'; 
     data=data+'<AltPhone>315-342-5468</AltPhone>'; 
     data=data+'<AltPhoneExt></AltPhoneExt>'; 
     data=data+'<Mobile>315-985-4984</Mobile>'; 
     data=data+'<MobileExt></MobileExt>'; 
     data=data+'<Fax>800-842-2346</Fax>'; 
     data=data+'<FaxExt></FaxExt>'; 
     data=data+'<EMail>[email protected]</EMail>'; 
     data=data+'<PotentialValue>900</PotentialValue>'; 
     data=data+'<Comment>We are looking for a reliable and prompt pest control provider.</Comment>'; 
     data=data+'<SourceCode>LEADFCTRY</SourceCode>'; 
     data=data+'<SourceDescription>Lead Factory Lead Distributors</SourceDescription>'; 
     data=data+'<SourceType>1</SourceType>'; 
     data=data+'<SourceClassCode>AGGREGATOR</SourceClassCode>'; 
     data=data+'<SourceClassDescription>Service Aggregators</SourceClassDescription>'; 
     data=data+'<TimeRange>9am-5pm</TimeRange>'; 
     data=data+'<SEligibleDate>10/1/08</SEligibleDate>'; 
     data=data+'<EEligibleDate>10/31/08</EEligibleDate>'; 
     data=data+'<LeadCost>27.00</LeadCost>'; 
     data=data+'<ProviderID>9843</ProviderID>'; 
     data=data+'<Questions>'; 
      data=data+'<QuestionNode>'; 
       data=data+'<Question>Have you previously had service done by Ace Pest Control?</Question>'; 
       data=data+'<Answer>No</Answer>'; 
      data=data+'</QuestionNode>'; 
      data=data+'<QuestionNode>'; 
       data=data+'<Question>Are you currently receiving service from any other pest control operator?</Question>'; 
       data=data+'<Answer>No</Answer>'; 
      data=data+'</QuestionNode>'; 
      data=data+'<QuestionNode>'; 
       data=data+'<Question>When was the first time you noticed your pest problem?</Question>'; 
       data=data+'<Answer>We first noticed the problem in July 2006 when we saw damage to one of the walls in the basement.</Answer>'; 
      data=data+'</QuestionNode>'; 
     data=data+'</Questions>'; 
    data=data+'</LeadInfo>'; 
data=data+'</LeadImportDatagram>'; 

if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
    //xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    xmlhttp=new ActiveXObject("MSXML2.ServerXMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() 
    { 
    alert("Ready state: "+xmlhttp.readyState) 
    alert("Status: "+xmlhttp.status) 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
     document.getElementById("myDiv").innerHTML=xmlhttp.responseXML; 
    } else if (xmlhttp.status==404) { 
     alert("XML could not be found"); 
    } 
    } 
xmlhttp.open("POST","http://www80.mypestpac.com/xml/importlead.asp",true); 
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
//xmlhttp.setRequestHeader("Content-type","text/xml"); 
xmlhttp.send(data); 
} 
</script> 
</head> 
<body> 

<h2>AJAX</h2> 
<button type="button" onclick="loadXMLDoc()">Request data</button> 
<div id="myDiv"></div> 

</body> 
</html> 

回答

0
data='<?xml version="1.0" encoding="utf-8" ?>'; 
data=data+'<LeadImportDatagram>'; 
    data=data+'<CompanyKey>302455</CompanyKey>'; 
    data=data+'<LeadInfo>;' 

13号线已成为

data=data+'<LeadInfo>'; 

代替

data=data+'<LeadInfo>;' 

,是造成你的XML错误。

+0

谢谢,我纠正了这个问题,现在得到这个错误“XMLHttpRequest无法加载http://www80.mypestpac.com/xml/importlead.asp.Access http://192.168.1.238是不允许的访问控制-Allow-Origin。“谷歌搜索并发现,jsonp是一个解决方案。但是我担心我不能将它用于json而不是用于XML响应。 –