XMLHttpRequest blocked in draw.io Electron app - javascript

I am desperatly trying to code a plugin for Draw.io that will basically do a request to another website in order to get some images.
It works fine on the local web version of Draw.io, but on the desktop version I get an empty responseText.
Here is what I'm simply doing in the plugin js:
Draw.loadPlugin(function(ui)
{
var data = null;
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://jsonplaceholder.typicode.com/posts");
xhr.setRequestHeader("accept", "application/json");
xhr.send();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
alert(this.responseText);
}
});
});
That is pretty basic and probably not working well but I should at least get an error... All I have is an empty response.
Does the Electron app blocks outside XHR? Is there a workaround? Thanks!

Related

custom thingsboard widget for device control (REST)

I'm creating a controller for lights using thingsboard.
I need to change device's telemetry data (thingsboard) using rest put request
$.post("http://<ip_here>:8080/api/v1/<device_accesscode_here>/telemetry",{ selectedPreset:2 });
REST calls work using swagger.io and postman, but when calling from widget or any other web page, request returns 400.
Can't seem to find solution to this and url is correct. i have tried both $.post and $.ajax styles.
YAY! i got it working!
for some reason, only XHR approach worked..
var data = "{\"selectedPreset\":\"2\"}";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "IP HERE");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "33c35ded-140d-e016-fa35-ee8185d7bd44");
xhr.send(mydata);
i ripped this right out of postman.

chrome extension http request to website

I wanted to make a chrome extension that gets the html code from this website: https://free-proxy-list.net/
without actually going to that webpage.
I tried using the steps here:
https://developer.chrome.com/extensions/xhr
but the request kept showing as undefined when I tried to print it out.
The script I was using was:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
xhr.open('GET', "url", true);
xhr.send(null);
document.write(xhr.send());
Note: not putting url in code since SO won't let me post a question with more than 2 links.
How would I get the code from this website in a string variable that I can parse?

ajax javascript request not working in Safari

I have this snippet below to fetch how score count on shared links on Reddit. Using Google Chrome I get a JSON response I can work with, but in Safari the same code sends me directly to "onerror" instead.
Help please :)
UPDATE 1
For some reason it doesn't work for me but for everyone else. I have all plugins disabled but can only get it to work in incognito mode.
UPDATE 2
It seems it affect some people using Safari, but not all. Safe to say, the fault must be in the browser and not in my code :)
var myurl = 'http://www.google.com';
var jsonURL = 'http://www.reddit.com/api/info.json?url=' + myurl;
var request = new XMLHttpRequest();
request.open('GET', jsonURL, true);
request.onload = function () {
console.log('ON LOAD ------------------------------');
var response = JSON.parse(request.response);
console.log(response);
};
request.onerror = function () {
console.log('ON ERROR ------------------------------');
console.log(request);
};
request.send();

QML - XMLHttpRequest User-Agent change

I'm trying to change User-Agent used by XMLHttpRequest.
This is my connect() function.
It works perfectly but User-Agent isn't being changed..
Someone knows why? (I've checked headers with Fiddler) :(
function connect() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
console.log('Trying to connect...')
} else if(xhr.readyState === XMLHttpRequest.DONE) {
console.log('DONE')
}
}
xhr.open("GET", "http://www.mywebsite/index.php?waddawda");
xhr.setRequestHeader('User-Agent','FAKE-USER-AGENT');
xhr.send();
}
Thanks for your help.
P.S. If it's not possible to do it with XMLHttpRequest please suggest me an alternative :) I need to do a GET request with a modified User-Agent.
It didn't work, because back then, setting the user agent was forbidden by specification. Nowadays it should work, because it has been allowed. It works in Firefox.
In some browsers (Chrome) it still (2023-02) doesn't work because of a long standing bug.

How to read JSON data, that is sent from Android, using Javascript?

I have created a mobile application that scans the surrounding Bluetooth devices and I am able to put the devices into an array list.
Now, using the http POST method, I have to send a JSONObject having this array list to a url and even for this I have written an expected code on the android app(I am sure this code will work because I have already worked on this using POST method to URL's and displaying the response on the activity).
But, how to listen the JSONObject, sent by any android app to the URL, parse it and show it on that particular URL's webpage ?
(In short I am looking for a Javascript code which can handle this and show the list.)
if you already have the URL where the JSON is being posted to you can do:
plain js:
var request = new XMLHttpRequest();
request.open('GET', 'URL', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var data = JSON.parse(request.responseText);
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
with jquery:
var getData = $.getJSON('URL');
getData.done(function(data){
// you have access to data here
});

Categories