JavaScript - Xmlhttprequest: Script Works To Request Xml File On Server But Not On External Server
I have the following JavaScript (see below). The script requests an XML file from the server and displays it on the page.
The script works fine when the requested XML file is stored on the same server as the script. The problem is when I try requesting an XML file from an external server such as the National Weather Service. I get an error. If I take the XML file from the National Weather Service and save it to my server it works. Why can't I use my script to request XML files stored on external servers? Thanks in advance for any help. Javascript Code Code: window.onload = initAll; var xhr = false; function initAll() { document.getElementById("makeTextRequest").onclick = getNewFile; document.getElementById("makeXMLRequest").onclick = getNewFile; } function getNewFile() { makeRequest(this.href); return false; } function makeRequest(url) { if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } if (xhr) { xhr.onreadystatechange = showContents; xhr.open("GET", url, true); xhr.send(null); } else { document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest"; } } function showContents() { if (xhr.readyState == 4) { if (xhr.status == 200) { if (xhr.responseXML && xhr.responseXML.contentType=="text/xml") { var outMsg = xhr.responseXML.getElementsByTagName("choices")[0].textContent; } else { var outMsg = xhr.responseText; } } else { var outMsg = "There was a problem with the request " + xhr.status; } document.getElementById("updateArea").innerHTML = outMsg; } } HTML Code Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My First Ajax Script</title> <script type="text/javascript" src="script01.js"></script> </head> <body> <p><a id="makeXMLRequest" href="http://www.weather.gov/xml/current_obs/KOJC.xml">Request an XML file</a></p> <div id="updateArea"> </div> </body> </html> Similar Tutorialswell i'm kind of new to this website and scripting with javascript but i am wondering: in a client side .js file i have some coding which results in a string which is stored in a variable called exportData now i want that string to be written into a text file on a server but how do i do that? it is not local on the client's pc and i can't find how to do it anywhere... can you guys help me? greetings, Fady I must make clear that I am not a javascript programmer; I am simply trying to get this script to work on my website. It functions perfectly locally (with all browsers), but after uploading to my website, it shows the controls at the top, but not the content. Below is the code for the FreeMind Flash Browser which can be found he http://freemind.sourceforge.net/wiki.../Flash_browser It requires 2 additional files, flashobject.js and visorFreemind.swf plus the FreeMind file (*.mm) that stores the content. The FreeMind file that has the content displays perfectly on localhost, but not on the website. I have all of these files in the same directory and have double checked everything, but it still doesn't function on my website. I have other flash files and scripts on the same website in other pages that function perfectly. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="description" content="freemind flash browser"/> <meta name="keywords" content="freemind,flash"/> <title>MINDMAPS</title> <script type="text/javascript" src="flashobject.js"></script> <style type="text/css"> /* hide from ie on mac \*/ html { height: 100%; overflow: hidden; } #flashcontent { height: 100%; } /* end hide */ body { height: 100%; margin: 0; padding: 0; background-color: #9999ff; } </style> <script language="javascript"> function giveFocus() { document.visorFreeMind.focus(); } </script></head> <body onLoad="giveFocus();"> <div id="flashcontent" onmouseover="giveFocus();"> Flash plugin or Javascript are turned off. Activate both and reload to view the mindmap </div> <script type="text/javascript"> // <![CDATA[ // for allowing using http://.....?mindmap.mm mode function getMap(map){ var result=map; var loc=document.location+''; if(loc.indexOf(".mm")>0 && loc.indexOf("?")>0){ result=loc.substring(loc.indexOf("?")+1); } return result; } var fo = new FlashObject("visorFreemind.swf", "visorFreeMind", "100%", "100%", 6, "#9999ff"); fo.addParam("quality", "high"); fo.addParam("bgcolor", "#a0a0f0"); fo.addVariable("openUrl", "_blank"); fo.addVariable("startCollapsedToLevel","3"); fo.addVariable("maxNodeWidth","200"); // fo.addVariable("mainNodeShape","elipse"); fo.addVariable("justMap","false"); fo.addVariable("initLoadFile",getMap("blues.mm")); fo.addVariable("defaultToolTipWordWrap",200); fo.addVariable("offsetX","left"); fo.addVariable("offsetY","top"); fo.addVariable("buttonsPos","top"); fo.addVariable("min_alpha_buttons",20); fo.addVariable("max_alpha_buttons",100); fo.addVariable("scaleTooltips","false"); fo.write("flashcontent"); // ]]> </script> </body> </html> Hey guys. Is there any any ANY way to read highlighted text of in an iframe that is displaying an external server / site? Say you select some text in the iframe and click a button outside of the iframe that prints the selected text to a textbox? Really really would love to do this or any ANY workaround. This really sucks that its a security measure by default that doesn't allow this!! Ugh! Ok, This one has plagued me for some time now. I am parsing an XML file for a search feature. On my local machine, the code below works fine in FF, but when I upload it, I get an error stating y[0] (the root element) is undefined. I think the problem may be with my onload method but I can't be sure. Code: function importXML() { if (document.implementation && document.implementation.createDocument) { xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.onload = createTable; } else if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState==4) createTable(); } } else { alert('Your browser can\'t handle this script'); return; } xmlDoc.load("catalog.xml"); } // and now for the part that fails var y = xmlDoc.getElementsByTagName('data'); var dcount = 0; for (t=0;t<y[0].childNodes.length;t++) { //this is the line where FF reports the error if (y[0].childNodes[t].tagName == "record") { dcount++; } } The xml syntax is as follows Code: <data> <record> <item 1>text</item 1> <item 2>text</item 2> <item 3>text</item 3> </record> <record> <item 1>text</item 1> <item 2>text</item 2> <item 3>text</item 3> </record> </data> I just can't figure out why this works locally and not on the server. IE works fine, but then again it uses the activeXObject method so it should function differently. Hi, Following this jQuery slideshow tutorial (Usecase 3 sample, about 2/3 down the page, this is the one without thumbnails): http://www.gcmingati.net/wordpress/w...vwt/index.html I constructed the slideshow: http://backstageweb.net/Salon/slideshowcode.htm ...which works perfectly in browser view locally (FF and IE). When uploaded to the server, however, I've got a static list of photos as you can see. Can someone shed some light on what the problem is? All relevant files are attached (I changed the images to simple colors to get the file size within the attachment limit). Thanks. John I need help to make a Server to server connection I already have a server connected to clients, clients send msgs and it echoes back to all of them and now i want when a client sends a msg it echoes on his server and the other server too .. so when any of the clients on any of the servers sends a msg it is broadcasted all over the servers to all clients This is my SERVER code Code: import java.io.*; import java.net.*; public class MultiThreadChatServer { // Declaration section: This part to declare the server socket, client // socket, input stream // and output stream static Socket clientSocket = null; static ServerSocket serverSocket = null; // server can hold up to 10 clients static clientThread t[] = new clientThread[10]; public static void main(String args[]) { int port_number = 6000; if (args.length < 1) { System.out.println("Server Started \n" + "Now using port number=" + port_number); } else { port_number = Integer.valueOf(args[0]).intValue(); } // Initialization section: Where I try to open a server socket on the // given port try { serverSocket = new ServerSocket(port_number); } catch (IOException e) { System.out.println(e); } // Create a socket object from the ServerSocket to listen and accept // connections // Open input and output streams for this socket will be created in // client's thread since every client is served by the server in // an individual thread while (true) { try { clientSocket = serverSocket.accept(); for (int i = 0; i <= 9; i++) { if (t[i] == null) { (t[i] = new clientThread(clientSocket, t)).start(); break; } } } catch (IOException e) { System.out.println(e); } } } } // This client thread opens the input and the output streams for a particular // client, // ask the client's name, informs all the clients currently connected to the // server about the fact that a new client has joined the chat room, // and as long as it receive data, echos that data back to all other clients. // When the client leaves the chat room this thread informs also all the // clients about that and terminates. class clientThread extends Thread { DataInputStream is = null; PrintStream os = null; Socket clientSocket = null; clientThread t[]; public clientThread(Socket clientSocket, clientThread[] t) { this.clientSocket = clientSocket; this.t = t; } public void run() { String line; String name; try { is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); os.println("Enter your name."); name = is.readLine(); os.println("Hello " + name + " you can now start chatting with all the connected chat-mates"); for (int i = 0; i <= 9; i++) if (t[i] != null && t[i] != this) t[i].os.println(".." + name + " has entered the chat room .."); while (true) { line = is.readLine(); if (line.startsWith("/quit")) break; for (int i = 0; i <= 9; i++) if (t[i] != null) t[i].os.println("<" + name + "> " + line); } for (int i = 0; i <= 9; i++) if (t[i] != null && t[i] != this) t[i].os.println("" + name + " has left the chat room .."); os.println("Bye " + name + " .."); // Clean up: // Set to null the current thread variable such that other client // could // be accepted by the server for (int i = 0; i <= 9; i++) if (t[i] == this) t[i] = null; // close the output stream // close the input stream // close the socket is.close(); os.close(); clientSocket.close(); } catch (IOException e) { } ; } } I have a google blogger site on which I want to run a PHP script. The problem is that blogger doesn't have a PHP interpreter. So I was thinking, can I run the php file on a remote server that supports php, and load the result in a div in blogger? Thx! I recently coded a page that extracts certain nodes from an external XML file and prints a line of text on the webpage. It is an HTML page that is strictly devoted to this purpose. There is no other information on it. The page is currently stored locally on my computer, and when I open it, the script runs fine and outputs the correct text. However, when I transferred this script over to my web server, and locate it that way, it doesn't run correctly. When I go to the page, it is supposed to display a line of text at the top. It does when I run the file from my local machine, however when I open it from the server, it doesn't display this text. (Same browser, same computer) When I right-click and view the source, all the code is there, and the files are identical. Any idea as to why the script runs correctly only when I open the HTML page locally? I appreciate any help or advice. Thanks! Hi, I am hosting my website in free server (freehostia.com) and i am using joomla1.5 . I want to parse RSS from my other websites. Joomla does it with its modules. But the server do not allow outbound HTTP request, hence i had use rss-to-javascript website to parse RSS feeds. It parses well but there is its advertisement below the feed (which i think is nonsense). The link it provides is in the format of <script src="http://somethin.com/somdfile.php?parse=.."></script> when i put the link in my browser i see following document.write("string of my parsed xml file with advertisement at end"); What i want to do is get the sting of above line and edit it to chop off the end advertisement. if there was any method to load that output sting in any variable, the task would had been completed. If anybody knows how to do it, please help me. Hi, I am trying to access an XML file from a server in my JavaScript code. I have an XML file like this: -<stream version="1.2"> -<room id="simulator" time="128168557915"> -<dimention id=0 x="1.25" y="2.00"> <m mcu="160" sid="75"> </dimention> </room> -<room id="simulator" time="128168557928"> -<dimention id=0 x="1.95" y="1.86"> <m mcu="160" sid="55"> </dimention> </room> </stream> this file is generated by an application and I can access it from a URL ( since I am using the simulator for this application the XML is accessible from http://localhost:8081/feed/demo) This xml file is updated every few seconds and constantly growing. I have a javascript code which I've added the following code to it in order to use the data from XML file: <script type="text/javascript"> if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","http://localhost:8081/feed/demo",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.write("<table border='1'>"); var x=xmlDoc.getElementsByTagName("room"); for (i=0;i<x.length;i++) { document.write("<tr><td>"); document.write(xmlDoc.getElementsByTagName("dimention")[i].getAttribute("x")); document.write("</td><td>"); } document.write("</table>"); </script> Now here comes my problem: if I have the XML file saved on same drive as html page and I address it like this: xmlhttp.open("GET","floor.xml",false); it works fine, but when I pass the URL it doesn't. is there anything else I should do in case of loading the xml from URL? my second question is that I want to use the text values returned by xmlDoc.getElementsByTagName("dimention")[i].getAttribute("x") in an if statement like this : if (valuereturned = 2.00) { do sth } what is the best way to do that, since the returned value is a text. I need the answer ASAP and I really appreciate your help, thanx :-) So, this is my last problem. If I can nail this, then the website will be completely AJAX driven, and will be spectacular. It took me a while to figure out just how to ask the question. lol Now it is really simple. 1) When I hit the button, in the simplified example below, the website sends a code to the server. 2) The server then re-writes the "xyz.js" JavaScript file with a new array of values. Is there any way to re-retrieve that JavaScript file, from the server to make it the current "xyz.js" rather than the old one that loaded on the client originally. I can get a new XML data file and re-submit that to the website, but how do I re-retrieve the new JavaScript code? Code: <html> <head> <script src="xyz.js" type="text/javascript"></script> </head> <body> <button onclick="onload();">Change</button> </body> </html> I am wanting to setup a dependant drop down where when a user clicks on the first drop down, it changes the contents of the second. The way I intend to do this is via a csv file. I have found several scripts but I have been unable to get them to work. Two of which a http://answers.yahoo.com/question/in...3114347AA7GYJ7 http://purbayubudi.wordpress.com/200...ng-javascript/ The first is the better option from what I can see. but the problem is readyState seems to be always 1. I am wondering if someone could perhaps give me a few moments to see if I can get this to work or suggest a better solution. I don't want to use a database approach, at least not yet. Dear friend , I am uploading text file on html page , that text file lies on desktop and getting updated at every hour . I will keep my html page on server so how will it upload that text file from my desktop ? it is being generated by one cloud sensor . I'm attempting to pull the hidden iframe file upload trick using javascript. I've go my form file upload and iframe fine, but I can't figure out what to do when it gets server side. There seems to be relatively no documentation on how to handle uploaded files in server-side javascript. Ideas? I'm at a loss.
Hello, Is there a way using javascript to have it check if a file exists on the server? I'm trying to check for a file on my server without having to refresh the open webpage. If this can't be done with javascript, do you know which webpage languange can execute this? Thanks Hi all, I would like to have an HTML button which, when clicked by the user, contacts the server and runs a Python script on the server. When the Python script finishes running, the current HTML page automatically reloads a file from the server. Is that possible? YJ Hi there, Can anybody help me to write php/javascript code which will allow users to open files directly from web browser into desktop application? Here is the specification: I have a photo editing business with many people working in Photoshop. I am currently developing a web based application (joblist) using Javascript and PHP which should allow the photoshop designers to browse and open files/images directly from joblist/web browser into photoshop. The reason I want this instead of browsing folder is that I have a database where I store who worked on which file, when and how long it took. The concept is that, designers will select a file and click on start, as soon as they click on start the original file will open in Photoshop and there will be an entry into database (using PHP). Once they finish the task they will close the file and click on Finish button. My joblist application will be published in a local server and the file will be open on a local network, so when they save the file it will be saved where the source file is located in (local server). The application should work in both PC and Mac. I have already done all other part of the application except file opening directly from browser to desktop application functionality. Anybody can help me to write the code (PHP or Javascript) which can open the file from browser (local server) directly into desktop application e.g. PHotoshop or Illustrator? Thank you very much I look forward to someone's real help! Best regards Mr. Sumon Hi there, I have a photo editing business with many people working in Photoshop. I am currently developing a web based application (joblist) using Javascript and PHP which should allow the photoshop designers to browse and open files/images directly from joblist/web browser into photoshop. The reason I want this instead of browsing folder is that I have a database where I store who worked on which file, when and how long it took. The concept is that, designers will select a file and click on start, as soon as they click on start the original file will open in Photoshop and there will be an entry into database (using PHP). Once they finish the task they will close the file and click on Finish button. My joblist application will be published in a local server and the file will be open on a local network, so when they save the file it will be saved where the source file is located in (local server). The application should work in both PC and Mac. Anybody can help me to write the code (PHP or Javascript) which can open the file from browser (local server) directly into desktop application e.g. PHotoshop or Illustrator? Thank you very much I look forward to someone's real help! Best regards Mr. Sumon www.clippingpathindia.com I heard there is something called XMLHttpRequest that is compatible to all browsers. What does that actually do? Is there something to do with Javascript? Thank you.
|