I am trying to load an xml file that is on my local system. But I always get Network_err. I do the following.
function LoadXmlDoc(dName)
{
var xhttp;
if(window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
try
{
xhttp.open("GET", "file.xml", false);
xhttp.send();
}
catch(e)
{
window.alert("Unable to load the requested file.");
return;
}
return xhttp.responseXML;
}
How can I load an xml file that is on my system. all files are in same folder on my pc. Thanks
Try:
function XMLDoc()
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
};
xmlhttp.open("GET","yourfile",true);
xmlhttp.send();
}
Updated due to simplify
Invoke XMLDoc() and pass your file uri instead of yourfile
Note: Don't forget to run this script on server
you might need to give proper path of xml file like this
xhttp.open("GET", "file:///C:/file.xml", false);
xhttp.send();
will do work fo ryou
full code will be like , Read more : Loading XML with Javascript
var xmlDoc;
var xmlloaded = false;
function initLibrary()
{
importXML("file:///C:/file.xml");
}
function importXML(xmlfile)
{
try
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", xmlfile, false);
}
catch (Exception)
{
var ie = (typeof window.ActiveXObject != 'undefined');
if (ie)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
xmlDoc.load(xmlfile);
readXML();
xmlloaded = true;
}
else
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = readXML;
xmlDoc.load(xmlfile);
xmlloaded = true;
}
}
if (!xmlloaded)
{
xmlhttp.setRequestHeader('Content-Type', 'text/xml')
xmlhttp.send("");
xmlDoc = xmlhttp.responseXML;
readXML();
xmlloaded = true;
}
}
You can't using XHR due security reasons.
Check this post is very complete answer for you.
Then ckeck the HTML5 API for local files: http://www.html5rocks.com/en/tutorials/file/filesystem/
Related
function getXmlHttpRequestObject()
{
var xmlHttp = false;
if (window.XMLHttpRequest)
{
return new XMLHttpRequest(); //To support the browsers IE7+, Firefox, Chrome, Opera, Safari
}
else if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP"); // For the browsers IE6, IE5
}
else
{
alert("Error due to old verion of browser upgrade your browser");
}
}
var xmlhttp = new getXmlHttpRequestObject(); //xmlhttp holds the ajax object
function servletPost()
{
if(xmlhttp)
{
var comp_to = document.getElementById("comp_to").value;
var comp_subject = document.getElementById("comp_subject").value;
var comp_letter = document.getElementById("comp_letter").value;
var date_time = document.getElementById("date_time").value;
if(comp_to==""||comp_subject==""||comp_letter==""||date_time=="")
{
document.getElementById("redSignal").style.display='block';
document.getElementById("redSignal").innerHTML="All Fields are necessary";
}
else
{
xmlhttp.open("POST","complaintHandler",true);
xmlhttp.onreadystatechange = handleServletPost;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data_string="to="+comp_to+"&subject="+comp_subject+"&complaint="+comp_letter+"&date_time="+date_time;
xmlhttp.send(data_string);
}
}
}
function handleServletPost()
{
if (xmlhttp.readyState == 4)
{
if(xmlhttp.status == 200)
{
document.getElementById("greenSignal").style.display='block';
document.getElementById("greenSignal").innerHTML=xmlhttp.responseText;
}
else
{
document.getElementById("redSignal").style.display='block';
document.getElementById("redSignal").innerHTML="Error Code ="+xmlhttp.status;
}
}
}
I am getting the problem of error code 404
What could be the problem in this code? Please help me.
Error 404 itself says that your URL is wrong .
xmlhttp.open("POST","complaintHandler-wrong",true);
check this URL 1st .
ERROR 404 Says. The requested Http request is not present or wrong.
Please check your "complaintHandler" It might be complaintHandler.jsp , kind of...
Please go through this tutorial for your future use.
In Prestashop I am using alphabitical search module. When I installed the module on my prestashop theme(1.5.0.17) I got an error like
Uncaught TypeError: Cannot call method 'getElementsByTagName' of null
Here is the code for jQuery file
function initXMLHTTPRequest()
{
var xmlHttp = null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function getProdList(id,langid)
{
var xmlHttp=initXMLHTTPRequest();
var str = "value="+id+"&langid="+langid;
var span="";
var url = "modules/alphabetsearch/PHP/searchDetails.php?";
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 )
{
span=span+"<ul style='padding-bottom:20px;'><li><b>"+id+"</b></li>";
var xmldata=xmlHttp.responseXML;
var xmlObj = xmldata.getElementsByTagName("ProductDetails")[0];
var menusize= xmlObj.childNodes[0].childNodes[0].childNodes[0].nodeValue;
var xmlObjlength = xmlObj.childNodes.length;
for(var i=1;i<xmlObjlength;i++)
{
var ProductName=xmlObj.childNodes[i].childNodes[0].childNodes[0].nodeValue;
var productId=xmlObj.childNodes[i].childNodes[1].childNodes[0].nodeValue;
span=span+"<li style='line-height:20px;display:block;height:20px;list-style:none;border-bottom:1px solid #666666;'><a href='product.php?id_product="+productId+"'><div>"+ProductName+"</div></a></li>"
}
if(xmlObjlength==1)
{
document.getElementById("products").innerHTML="";
document.getElementById("products").innerHTML="No Products Under this Alphabet";
}
else{
document.getElementById("products").innerHTML="";
span=span+"</ul>";
document.getElementById("products").innerHTML=span;
}
}
};
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", str.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(str);
}
Can someone kindly tell me how to solve this issue? Any help and suggestions will be really apprecaible. Thanks
The live site can be found here
How can I read a text file located in relative path of my site using javascript?
This is what I been trying to do and it says access denied:
this.load = function(path){
if(root == null){
root = path;
}
var client = new XMLHttpRequest();
client.open('GET', "assets/myTextFile.txt");
client.onreadystatechange = function() {
alert(client.responseText);
};
client.send();
};
If you use jquery:
$.ajax({
url: "assets/myTextFile.txt",
success: function(data){
alert(data);
}
});
Please try this code:
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","assets/myTextFile.txt",true);
xmlhttp.send();
I am working in PHP I have created a page here I have 2 combo box when I select first combo box item the second one is filled according the first combobox using JavaScript the problem I am facing is where I am trying to save the data of second combo box in database I found null value. transition_party_id is the combobox that fill dynamically here I code
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getparty(category) {
var strURL="../admin/get_partyname.php?category="+category;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('partydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
req.open("GET", strURL, true);
req.send(null);
You send 'null' post data by "GET" method.
Or you waiting data in get?
One of my client had a form developed in component Mosets Tree. In the form when you select a main category it automatically displays the subcategories. Now the issue is; I had to hide some code to stop displaying a few things, after that the java script which was displaying subcategories after we select the main category is not working in IE.
code:
var xmlhttp;
function stateChanged(){
if (xmlhttp.readyState==4) {
document.getElementById("subCatId").innerHTML = xmlhttp.responseText;
}
}
function fnGetSubCategory() {
xmlhttp = GetXmlHttpObject();
var new_cat_id = document.getElementById("new_cat_id").value;
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return true;
}
var url="ps.php?cat_id="+new_cat_id;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function GetXmlHttpObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
It is working fine in all other browsers.
Thanks in advance.
Try creating the object xmlHttp with this code:
function createXmlHttpRequestObject(){
var xmlHttp;
try{
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// If its IE 6 or other version before
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
// We try all versions
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
try {
//Try creating xmlHttp object
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e){
xmlHttp = false;
}
}
}
// If object doesn't exist sends error
if (!xmlHttp){
alert("Error creating XMLHttpRequest object");
}
else{
return xmlHttp;
}
}