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();
Related
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!
I have recently learned to use APIS in code, but for my javascript version I've had to learn different ways of doing it, when using what should be the simplest method nothing gets outputted to my chrome console.
const request = new XMLHttpRequest();
request.open('GET','apistuff, true);
request.onload = function() {
let data = JSON.parse(this.response);
console.log(data);
};
HTML file is just empty and calls the javascript file
Just replace current code with below code and let me know the status
const request = new XMLHttpRequest();
request.open('GET', apistuff, true);
request.send();
// we need to call this send method to open breach between server and client
request.onload = function() {
let data = JSON.parse(request.response);
// if response is in text simply use **request.responseText**
console.log(data);
};
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.
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
});
I'm trying to make a little script to rehost pictures on the web on imgur.
This is called Image Sideloading and you only need to point the browser to http://api.imgur.com/2/upload?url= + Picture's Url
It doesn't return any XML or JSON response so I can't parse it to get the img URL.
But I think I found a way to do this but can't get it to work properly. Here's the code I'm using.
$(document).ready(function() {
$("#button").click(function() {
str = $("input").val().toString();
link = "http://api.imgur.com/2/upload?url=" + str;
var xhr = new XMLHttpRequest();
xhr.open("GET", link);
xhr.send();
var headers = xhr.getAllResponseHeaders().toLowerCase;
alert(headers);
});
});
And looking at Google Chrome's console these are the results generated after the script runs.
I am not allowed to post images yet so here's a link to the results: http://i.imgur.com/xCyIP.png
I need to somehow access that 4th response header because even though this method doesn't return any parsable XML or JSON response that link is the uploaded img's URL which is all I need.
So is there a way to access that info? Why is it cancelled?
Thanks everyone!
Well, the API must return something, else it is useless.
Actually, http://api.imgur.com/examples advises you to do something like:
var xhr = new XMLHttpRequest();
xhr.open("GET", link);
xhr.onload = function() {
var url = JSON.parse(xhr.responseText).upload.links.imgur_page;
alert(url);
}
xhr.send();
Edit. OK, I got it. Normally, the above code should work, but I think imgur is facing a problem. I will report the bug.
While the bug is still there, here is a dirty solution to serve your needs:
var xhr = new XMLHttpRequest();
xhr.open("GET", link);
xhr.onload = function() {
html = xhr.responseText;
alert(html.substr(html.indexOf('http://i.imgur.com/') + 19, 5));
}
xhr.send();
You code wasn't working because you weren't waiting for the answer. You have to catch it through a callback function.
Edit. The above code only works locally. As you're using jQuery, let me introduce the shortcut $.post:
$(document).ready(function() {
$("#button").click(function() {
$.post('http://api.imgur.com/2/upload.json', {
key: 'ec575de603b936e54c2b4a9f232d537e',
image: $("input").val().toString()
}, function(data) {
alert(data.upload.image.hash);
});
});
});
http://jsfiddle.net/Le_Sphinx/rEfdS/