Javascript XMLHttpRequest Invalid Argument - javascript

I'm currently working on a project trying to update a page reading from an XML file on our intranet server. After doing some working I came up with the following code:
// IE7+
if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); }
// IE6, IE5
else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.open("GET", "VerifiedUrl+XML.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
if (xmlDoc.getElementsByTagName("CheckBox")[0].childNodes[0].nodeValue == "True"){
document.getElementById("PartToUpdate").innerHTML = xmlDoc.getElementsByTagName("TextBox")[0].childNodes[0].nodeValue;
}
Now I've tested this code on my localhost and it does in fact read from the correct file, displaying the updated information, but when I deploy it to the intranet, I get an "Invalid Argument" error. (The XML file itself has been deployed and is referencing correctly).
Edit: I've recently found the problem, in that the path I was referencing apparently couldn't find the file itself. So that brings up another question that someone might be able to shed light on:
//When referencing a file within the same folder, it works correctly.
xmlhttp.open("GET", "Sample.xml", false);
//However, if I include the full path, it doesn't read correctly. (Double slashes to escape correctly)
xmlhttp.open("GET", "\\\\Full\\Server\\Path\\Here\\Sample.xml", false);
Perhaps someone could shed some light on this?

should your path be something like this:
xmlhttp.open("GET","./Path/Here/books.xml", false); //for relative urls
xmlhttp.open("GET","http://localhost/Path/Here/books.xml", false); //for absolute urls
and if its a non-http synchronous request
var request = new XMLHttpRequest();
request.open('GET', 'file:///home/user/file.json', false);
its not similar to your system path.

The path here is wrong:
xmlhttp.open("GET", "\\\\Full\\Server\\Path\\Here\\Sample.xml", false);
You are using the wrong type of slashes for the internet, they would be correct on a file system. It needs to use a forward slash.
xmlhttp.open("GET", "//Full/Server/Path/Here/Sample.xml", false);

Have you checked the same-origin policy?

Related

Access local file from browser using jquery or Javascript

I'm trying to access local file from Browser. The file is located in Client Computer under C drive.
I tried different solution but all gave me error Access is denied.
I know that almost all browsers have disabled this functionality, but isn't there any setting we can do in browser or in client computer to make it work.
Someone told me to install browser extension to make it work, but I don't know how to do that.
try {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
var lines = xmlhttp.responseText; //*here we get all lines from text file*
alert(lines);
}
}
xmlhttp.open("GET", "file:///E:\file.txt", true);
xmlhttp.send();
} catch (e) {
alert(e);
}
Any help will be highly appreciated.
Thanks
Unfortunately no, for security reasons you cannot automatically load a file from the user's Hard Drive.
What you can do is create an <input name="myFile" type="file"> and make the user manually select the file you need. This is the only way

How to manually enter POST data into browser

I'm debugging my php script and need to try to post data to by manually inputting it into the URL in my browser.
The javascript which sends the request is below. How do I enter the correct data into my browser so it's encoded in the same way as the javascript function? I tried encoding the string with http://meyerweb.com/eric/tools/dencoder/ and putting sendmail.php?q="the encoded string"... but that didn't work. Do I have to add more information?
function SendPHP(str, callback){
xmlhttp = new XMLHttpRequest();
str = "q=" + encodeURIComponent(str);
xmlhttp.open("POST","sendmail.php", true);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4){
inProgress=false;
if(xmlhttp.status == 200){
callback(xmlhttp.responseText);
}
}
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
if (inProgress==false){
inProgress=true;
xmlhttp.send(str);
}
else{
writeDisplayConsole("ERROR: xmlhttp fired twice!");
}
}
Use the Chrome Rest plugin extension https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
You may want to look at Fiddler - the web debugging proxy. https://stackoverflow.com/questions/tagged/fiddler
One of Fiddler's features is Composer, which lets you edit a previously seen request, or create a new one from scratch. Check out e.g. this answer, it deals with a very similar issue: check POST request with Fiddler

Why does my AJAX loading fail the first time through when hosting my MVC3 project in IIS7?

I was writing an mvc application with a few tabs. I noted that when hosted on IIS 7, the home page has a link triggering a JavaScript function to load content via AJAX.
It doesn't work on the first page load, however when I visit some other tab and come back to the home page and click on the link it works perfectly. Can someone tell me the reason for this or how to avoid it?
The Loading Code
function GetLabels(project) {
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
document.getElementById("light").innerHTML = xmlHttp.responseText;
}
}
xmlHttp.open("GET", "/Home/GetLabels?project="+project, true);
xmlHttp.send();
document.getElementById('light').style.display = 'block';
document.getElementById('fade').style.display = 'block';
document.getElementById("light").innerHTML =
"<img src='Content/load.gif' alt='Please wait' />";
}
The Link that Triggers it
Click here
You should not hardcode urls like this:
xmlHttp.open("GET", "/Home/GetLabels?project="+project, true);
You should always use url helpers to generate them:
xmlHttp.open("GET", "#Url.Action("GetLabels", "Home")?project=" + encodeURIComponent(project), true);
Now your AJAX request will work no matter whether you are hosting in Visual Studio's built-in server or IIS. The reason why your code doesn't work in IIS is because in IIS your application is hosted ni a virtual directory that you must include in your url. So the correct url is not /home/getlabels but /appname/home/getlabels which is something that the url helper takes into account.
Also since you are using a GET request the web browser might cache the result and never send a request again to the server. To avoid this you should append a random query string parameter to the url or use the POST verb.

SQL query using AJAX in Wordpress theme

I'm attempting to use AJAX to insert data into a database, but am unable to connect to the .php file on the server end. I'm thinking this is due to the fact that this is all run on Wordpress, so my normal url path to the .php file might not be correct.
Here is my setup:
Template file:
$('#newsletter-register').submit(function(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
alert(ajaxRequest.responseText);
}
}
ajaxRequest.open("GET", "register_newsletter.php", true);
ajaxRequest.send(null);
});
'#newsletter-register' is form that the user will use to submit his/her information. The alert when the ajaxRequest is at readyState 4 is thrown correctly, but will an empty value.
My php is a simple echo returning a string to signal that the connection is correct (presently I'm just trying to see if the files are correctly calling eachother).
I'm thinking it must be the url path to register_newsletter.php as I've tried placing the code within the template file, outside, etc.
My javascript file is located in /theme/assets/js/code.js where as my template files (including the register_newsletter.php file) in /theme.
Any ideas?
I had this type of issue yesterday. In my situation the page would only be accessed from
www.site.com/aFolder/
The script was stored in my
'/wp-content/themes/themeName/processForm.php'
folder.
and the URL I used was:
../wp-content/themes/themeName/processForm.php
My JS was simply in the head of the main header.php file, but note that the URL I had to use was relative to the URL the page the script was accessed from.
Does that shed any light on things?
EDIT:
As troubleshooting steps, try ensuring you can access your php file from it's absolute URL, and have a look in firebug to see what link is being called when you fire the ajax request to see if it matches up with what you expect.

By using JS, it is possible to get a page source, and then respond users with this getting page

I want to implement the following things by using JavaScript:
using AJAX to get a page source;
put in some data into this page source;
show the changed page to users.
So it is possible? If so, how?
By the way, I cannot use server side technologies. And if JS is not suitable for it, what client technologies can be used in this case?
Assuming that the page you are wanting to get source from is on the same domain, you can get the page source like this:
if("XMLHttpRequest" in window){
xmlhttp=new XMLHttpRequest();
}
if("ActiveXObject" in window){
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
xmlhttp.open('GET', "URLofFileYouWantToGetTheSourceFrom", true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.responseText);
}
};
xmlhttp.send(null);

Categories