i was trying to write a javascript program which read list of token from the configuration file and set the token. But javascript program failed to read my local configuration file.
javascript program below
require([
"splunkjs/mvc",
"splunkjs/mvc/simplexml/ready!"
],
function(mvc) {
var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
alert("start");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.status == 200) {
var allText = xhttp.responseText;
alert('Text = ' + allText)
}
};
xhttp.open("GET", "rejected_panels.conf", true);
xhttp.send();
alert("end");
defaultTokenModel.set('home_panel_1', 'yes');
});
I am accessing javascript from splunk's simple xml as below
<form script="rejected_panels.js">
....
....
</form>
I got this error in chrome browser
rejected_panels.js:21 GET http://localhost:8000/en-GB/app/multi_tOP/rejected_panels.conf 404
Kindly Help me on this.
Related
I have a simple code in a Javascript file that calls a PHP file using Ajax. At the moment it's a simple call and I don't need to pass any variables.
I'm using IIS and the folders structure is the following:
ROOT > sendMail.php
ROOT > JS > chat.js
The call starts from chat.js and it's the following:
function sendEmail(params){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("POST", "sendMail.php?q=" + params, true);
xmlhttp.send();
}
But I receive "404 Not Found". Someone can help me?
Thanks in advance!
I am trying to read a XML file with Javascript , it is for a assignment in school so i cant use jQuery it has to be Javascript. Basicly i can read ONE value but not the other
This is my JS that is resposible for reading the XML value. It will read startLng but it gives me startLat is undefined. But if i check the XML file the startLat is not undefined. I cant see what the problem is here.
What i am trying to do is to get the LatitudeDegrees and LongitudeDegrees from the XML file. But it only gives me the LongitudeDegrees and says the LatitudeDegrees is undefined. What am i doing wrong here?
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
getLatLang(xmlhttp);
}
};
xmlhttp.open("GET", "G1.TCX", true);
xmlhttp.send();
}
function getLatLang(xml)
{
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("Trackpoint");
startLat = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LatitudeDegrees")[0].childNodes[0].nodeValue;
startLng = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LongitudeDegrees")[0].childNodes[0].nodeValue;
}
This is the XML file
<Trackpoint>
<Time>2008-10-28T15:58:22Z</Time>
<Position>
<LatitudeDegrees>59.4111992</LatitudeDegrees>
<LongitudeDegrees>13.5304104</LongitudeDegrees>
</Position>
<AltitudeMeters>85.6945801</AltitudeMeters>
<DistanceMeters>0.2149343</DistanceMeters>
<HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
<Value>116</Value>
</HeartRateBpm>
<SensorState>Absent</SensorState>
<Extensions>
<TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2" CadenceSensor="Footpod"/>
</Extensions>
</Trackpoint>
This has been solved.
The problem was i did not have a document load.
I am using the jquery.csv-0.71.min.js lib to load a csv file and convert it to an array. However, when loading my webpage:
<script src="assets/lib/jquery/dist/jquery.min.js"></script>
<script src="assets/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Jquery-csv -->
<script src="assets/js/jquery.csv-0.71.min.js"></script>
<script>
(function() {
var data;
var url = 'data/data.csv';
function makeRequest(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.readyState === 4 && xhr.status === 200) {/*run code*/}
};
xhr.send(null);
}
})();
data = CSV.toArrays(xhr.responseText);
console.log(data);
</script>
I get in the console:
ReferenceError: CSV is not defined at 'data = CSV.toArrays(xhr.responseText);'
Any recommendations what I am doing wrong?
I appreciate your replies!
UPDATE
I put my code into the document read funciton, however, I still get the same error as above.
$(document).ready(function() {
(function() {
var data;
var url = 'data/data.csv';
function makeRequest(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.readyState === 4 && xhr.status === 200) {/*run code*/
}
};
xhr.send(null);
}
})();
data = CSV.toArrays(xhr.responseText);
console.log(data);
});
You code is executed before browser loads the CSV script.
Wrap you JavaScript code with
$( document ).ready(function() {
// PUT YOUR JavaScript CODE HERE
});
This will make browser to wait, until all your scripts are loaded, and only after to execute the code.
You can check the documentation of the jquery csv here. It says that you should invoke methods like this:
$.csv.toArrays(csv);
I want to upload a file trough a XMLHttpRequest. i have looked everywhere for examples and found quite a few. But i cant figer out what it is i am doing wrong. This is my code. The function is triggerd when a button is pressed. It not wrapped in from tags
function upl_kost() {
var url = "proces_data.php?ref=upload_kost";
var hr;
var file = document.getElementById("file_kost");
var formData = new FormData();
formData.append("upload", file.files[0]);
if (window.XMLHttpRequest) {
hr=new XMLHttpRequest();
} else {
hr=new ActiveXObject("Microsoft.XMLHTTP");
}
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "multipart/form-data");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
alert(return_data);
}
}
hr.send(formData);
}
and this function catches it.
if($_GET['ref'] == 'upload_kost') {
var_dump($_FILES);
}
My problem is that the $_FILES stays empty. When i look at the file.files variable in the js its loaded with the data from the file that i am trying to upload.
Thanks!
Reduce your JavaScript down to minimum required for this, then add in some helpful messages you can look in your console for
function upl_kost() {
var xhr = new XMLHttpRequest(),
url = 'proces_data.php?ref=upload_kost',
fd = new FormData(),
elm = document.getElementById('file_kost');
// debug <input>
if (!elm)
console.warn('Element not found');
else if (!(elm instanceof HTMLInputElement))
console.warn('Element not an <input>');
else if (!elm.files || elm.files.length === 0)
console.warn('<input> has no files');
else
console.info('<input> looks okay');
// end debug <input>
fd.append('upload', elm.files[0]);
xhr.addEventListener('load', function () {
console.log('Response:', this.responseText);
});
xhr.open('POST', url);
xhr.send(fd);
}
If you're still having a problem, it may be server-side, e.g. are you performing a redirect before trying to access $_FILES?
Your problem is that you're setting the content type of the request
hr.setRequestHeader("Content-type", "multipart/form-data");
If you ever saw a multipart/formdata post you'll notice the content type header has a boundary
Content-Type: multipart/form-data; boundary=----webko2354645675756
which is missing from your code.
If you do not set the content type header the browser will correctly set it and the required boundary. This will allow the server to properly parse the request body.
My web server sends a xmlhttprequest to write a JSON file whenever a button is clicked. However, the file has only read permissions when written. Also, this ONLY happens when the web server asks the cgi script to execute. However, if I try executing this cgi script myself from command line, the file is written with both read and write permissions. Is there any way I can allow the web server to make this cgi script write a file with full permissions?
As requested, here is the part of my code which sends the xmlhttprequest and fetches/the json file.
<script>
function loadXMLdoc() {
var xmlhttp = new XMLHttpRequest();
var url = "/cgi-bin/run.cgi"
var id = document.getElementById("inputId").value;
var mutation = document.getElementById("inputMutation").value;
var position = document.getElementById("inputPosition").value;
var json;
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
$.getJSON("data.json",function(result){
$.each(result, function(i, field){
output = "<h1>Output<h1>"
for (var f in field){
output+="<p>Identifier: " + field[f]["value"]+" Position: "+field[f]["position"]+" Mutation: "+field[f]["mutation"]+"</p>"
}
});
});
document.getElementById("test_container").innerHTML = output
}
}
xmlhttp.open("GET", url+"?id=" + id + "&mutation=" + mutation + "&position=" + position, true);
xmlhttp.send()
}
</script>
And here's the part in run.cgi which writes the file.
write_file = open("%s/data.json" %(root_directory), 'w')
write_file.write('{"results": [\n')
for i,result in enumerate(results):
write_file.write('\t{"value": "%s", "mutation": "%s", "position": "%s"}' % (result["value"], result["mutation"], result["position"]))
if i != len(results) - 1:
write_file.write(",")
write_file.write("\n")
write_file.write("]}")
write_file.close()