Reputation: 345
I've been testing my JS code on different browsers but it doesn't seem to work on some of them and on mobile either.
JS
function req1() {
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(json => {
var title = json.title;
var body = json.body;
document.getElementById("newsTitle").innerHTML = title;
document.getElementById("newsContent").innerHTML = body;
document.getElementById("newsContent2").innerHTML = body;
});
}
req1();
By reading this question I understood that the problem could be related to '=>' because it is a ES6 element and not all the browsers support it. But as you can see here it seems to be the way to get those json data: https://jsonplaceholder.typicode.com/
Is there a way to avoid using '=>' in this function to make it work on all the browsers?
Here the error that I get on Safari 9 for example:
I tried some solutions but now I get this error:
posts are not printed yet, any idea?
Upvotes: 1
Views: 207
Reputation: 7991
you need use fetch polyfill and old function syntax not the new arrow function syntax of es6.
function req1() {
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(function (res) {return res.json()})
.then(function (json) {
var title = json.title;
var body = json.body;
document.getElementById("newsTitle").innerHTML = title;
document.getElementById("newsContent").innerHTML = body;
});
}
req1();
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.4/fetch.js"></script>
<div id="newsTitle"></div>
<div id="newsContent"> </div>
Browser Support For Fetch API polyfill
Upvotes: 1
Reputation: 68933
Change the following lines
then(response => response.json())
To
.then(function(response){ response.json() })
And
.then(json => {
To
.then(function (json) {
Full Code:
function req1() {
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(function(response){ response.json()})
.then(function (json){
var title = json.title;
var body = json.body;
document.getElementById("newsTitle").innerHTML = title;
document.getElementById("newsContent").innerHTML = body;
document.getElementById("newsContent2").innerHTML = body;
});
}
req1();
Upvotes: 0
Reputation: 10096
Just use normal function syntax instead of ES6 arrow-syntax:
function req1() {
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(function (res) {return res.json()})
.then(function (json) {
var title = json.title;
var body = json.body;
document.getElementById("newsTitle").innerHTML = title;
document.getElementById("newsContent").innerHTML = body;
document.getElementById("newsContent2").innerHTML = body;
});
}
req1();
Most browsers that don't support ES6 arrow-syntax are unlikely to support the Fetch API. For those I would suggest using another form of HTTP request, or using a Polyfill, like GitHub's
Upvotes: 2
Reputation: 2499
Why not to use a simple request
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "https://jsonplaceholder.typicode.com/posts/1", false);
xhReq.send(null);
var serverResponse = xhReq.responseText;
var json = JSON.parse(serverResponse);
alert(json.title);
Upvotes: -1
Reputation: 7303
Use a normal function instead of a lambda:
"use strict";
function req1() {
fetch('https://jsonplaceholder.typicode.com/posts/1').then(function(response){
return response.json();
}).then(function(json){
var title = json.title;
var body = json.body;
document.getElementById("newsTitle").innerHTML = title;
document.getElementById("newsContent").innerHTML = body;
document.getElementById("newsContent2").innerHTML = body;
});
}
req1();
Just write
.then(function(json){
Instead of
.then(response => response.json())
Also, if you're not sure about the ES6 Syntax in your script, you can use something like babel:
Upvotes: 1