语法错误意外标记“>”

Syntax error unexpected token '>'

我一直在不同的浏览器上测试我的 JS 代码,但它似乎在其中一些浏览器和移动设备上都不起作用。

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();

通过阅读这个问题,我了解到问题可能与'=>'有关,因为它是一个 ES6 元素,并非所有浏览器都支持它。但是正如您在这里看到的那样,它似乎是获取那些 json 数据的方法:https://jsonplaceholder.typicode.com/

有没有办法避免在这个函数中使用'=>',使其适用于所有浏览器?

这里是我在 Safari 9 上遇到的错误,例如:

我尝试了一些解决方案,但现在出现此错误:

帖子还没有打印出来,知道吗?

更改以下行

then(response => response.json())

.then(function(response){ response.json() })

.then(json => {

.then(function (json) {

完整代码:

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();

使用普通函数而不是 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();

随便写

.then(function(json){

而不是

.then(response => response.json())

此外,如果您不确定脚本中的 ES6 语法,您可以使用 babel:

https://babeljs.io/repl/

只使用普通函数语法而不是 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;
      document.getElementById("newsContent2").innerHTML = body;
    });
}
req1();

大多数不支持 ES6 箭头语法的浏览器不太可能支持 Fetch API。对于那些我建议使用另一种形式的 HTTP 请求,或者使用 Polyfill,比如 GitHub's

为什么不使用简单的请求

 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);

你需要使用 fetch polyfill 和旧的函数语法,而不是 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>

浏览器支持 Fetch API polyfill

  • Chrome
  • 火狐
  • 野生动物园 6.1+
  • Internet Explorer 10+