Get data from JSON with XMLHtttpRequest - json

Can someone, please, explain to me, why in the code snippet below I get request.response == null?
In my JSON file I have an array with some data, which I want to use further.
After spending a lot of time on finding a problem, I found that there is a problem with link. When I uploaded JSON file into web service and used a link from that, it works fine. But when I use a link to the file in my file system, it doesn't want to work.
let requestURL = "../sliderContent.json";
const request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = () => {
if (request.readyState === 4 && request.status === 200) {
let slidesContent = request.response;
console.log(slidesContent);
returnSlidesReducer(slidesContent);
}
};

Related

How to get the latest release from Github API using JSON

I have tried to get the latest release from github. I think I may be reading the array wrong. What am I doing wrong here?
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var JS = JSON.parse(this.responseText);
console.log(JS.tag_name)
}
};
xmlhttp.open("GET", "https://api.github.com/repos/NAME/REPO/releases", true);
xmlhttp.send();
With the response from the console:
undefined
I am new to JSON so I am a little confused on how to read an array.
/repos/NAME/REPO/releases returns an array of releases, and you want to get the first one with JS[0].tag_name.
If you just need the latest release, use /repos/NAME/REPO/releases/latest and keep JS.tag_name.

Adding a query to a HTML Page

I have built a Status Page for my servers with HTML and CSS, and instead of updating the Server Status every time one of them goes down, I was wondering if it's possible to add something to query the IP's of the server every 10m or so, and if the query fails, to turn the status button to RED.
Here's what I'm working with: https://status.floridastaterp.org
Any help is largely appreciated!
Thanks.
You can do it without PHP, for that use javascript and make a ajax call:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// You change the status of button
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
And to call it every 10min, warp your function with:
setTimeout(request(),1000);
function request(){
if(response == true){
// This makes it unable to send a new request
// unless you get response from last request
response = false;
var req = $.ajax({
type:"post",
url:"https://status.floridastaterp.org"
});
req.done(function(){
console.log("Request successful!");
// This makes it able to send new request on the next interval
response = true;
});
}
setTimeout(request(),1000);
}
request();

html <script> tag headers

I'm trying to require a script that is firewalled with a header authentication system and trying to find a way around it.
So far it's pretty evident that you can't add custom headers to the script tag its self but I have seen something about customizing the headers on the page before requesting or on the server side.
Until this point, I can't say I've seen any solid answers.
You can load it via xhr and eval() it in-page. For example with jQuery, you can use:
http://api.jquery.com/jquery.ajax/ - see beforeSend to set headers; use this to retrieve the file content.
Then use https://api.jquery.com/jquery.globaleval/ globalEval() to eval the gotten content in-page.
You could achieve the same with vanilla HttpRequest and eval(), but I was always too lazy to do it that way. Or maybe not... I just found a piece of code in the project I'm working:
var evalScript = function(e) {
var h = evalScript.node,
s = document.createElement("script");
s.type = "text/javascript";
s.text = e;
h.appendChild(s);
h.removeChild(s);
};
evalScript.node = document.getElementsByTagName("head")[0] || document.getElementsByTagName("*")[0];
// TODO: make async
function loadJs(js) {
var req = new XMLHttpRequest();
req.open("GET", js, false);
req.send(null);
evalScript(req.responseText);
}
Just add the headers to this.
Here's a simple Ajax function you could use to get the contents of the script:
function get(url, callback) {
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function() {
if(this.readyState === 4) {
if(this.status >= 200 && this.status < 400) {
callback.apply(this, [this.responseText, this]);
} else {
// something went wrong.
}
}
};
request.send();
}
Since you need to set custom headers, you'd also use the request.setRequestHeader method, like this:
function get(url, callback) {
var request = new XMLHttpRequest();
request.open("GET", url, true);
// BEGIN: CUSTOM HEADERS
request.setRequestHeader("Header-Name", "header/value");
request.setRequestHeader("Other-Header", "other/value");
// END: CUSTOM HEADERS
request.onreadystatechange = function() {
if(this.readyState === 4) {
if(this.status >= 200 && this.status < 400) {
callback.apply(this, [this.responseText, this]);
} else {
// something went wrong.
}
}
};
request.send();
}
And finally, you'd use the function, like this:
get("url/to/your/script", function(response) {
// perform checks...
window.eval(response);
});
WARNING: be very, VERY careful when using eval, don't ever eval something you don't trust and remember eval can be evil.

console logs 'The provider id_provider is not a valid one'

Trying to get head around when debugging a Viewer app. Chrome console shows 'The provider id_provider is not a valid one'. Any suggestion?
If options is like this:
var options = {
env: 'AutodeskProduction',
getAccessToken: getToken }
}
function getToken () {
var response;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
response = JSON.parse(
xhr.responseText);
return response.access_token;
}
if forcing 2.9, 'The provider id_provider is not a valid one'.
If forcing 2.7 or 2.8, not log but the viewer doesn't show up.
If no specified version, 'Warning : no access token is provided. Use built in token : YtTb8vRA4XQfTorjm9c8eVZJTYP6'.
it stops in Autodesk360App.js
var initialItem = app.getDefaultGeometry(geometryItems);
Chrome logs 'Uncaught TypeError: app.getDefaultGeometry is not a function'
If I directly feed token:
accessToken: 'MorPwhKARIS3VGIrcd3FrZSjsnOx5'
it works beautifully in 2.7,2.8 and 2.9. But if no version, it stops in Autodesk360App.js, the same as above.
Thank you!
Aren't you missing the url on the http request? Maybe something line:
function getToken() {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", '/yourTokenEndPoint', false);
xmlHttp.send(null);
var response = JSON.parse(xmlHttp.responseText);
return response.access_token;
}
For the Viewer, it must be synchronous.

how to send json data to server in cordova android

hi frnd form last 2 days i am trying to send JSON data to server but is not working i am posting my js file and check if any error. and i am try to send json data by using xmlHttprequest. and if any other function and any change i have to do then plz tell me. i am developing cordova project in eclipse.and if any other thing and any file also have to change then tell me
this is my js file and on click registration button i am calling this method.
function get() {
alert("function is called");
var name_field_value=document.getElementById("name_field").value;
var email_field_value=document.getElementById("email_field").value;
var password_field_value=document.getElementById("password_field").value;
var phone_field_value=document.getElementById("phone_field").value;
var JSONdata= {
"name": name_field_value,
"mobile_number": phone_field_value,
"email": email_field_value,
"password": password_field_value
}
var request = new XMLHttpRequest();
request.open("POST", "http://www.jiyonatural.com/AccountManagements/insert_new_user", true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
alert(request.status);//this alert is working and getting 0 status
if (request.status == 200 || request.status == 0) {
// -> request.responseText <- is a result
/*var tweets = JSON.parse(request.responseText);*/
alert(request.responseText);//this alert is not working//
//if i make other alert then it works
}else{
alert("function is called3");}
}
}
request.send(JSON.stringify(JSONdata));
}
Looks like your question is incomplete.
In my experience the error probably lies in server code friend.
And you should use ajax method in jquery for communication with server.its easier than XMLhttpRequest.