简体   繁体   English

语法错误意外标记“>”

[英]Syntax error unexpected token '>'

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代码,但它似乎在某些浏览器和移动浏览器上均不起作用。

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

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. 通过阅读此问题,我了解到该问题可能与“ =>”有关,因为它是ES6元素,并非所有浏览器都支持。 But as you can see here it seems to be the way to get those json data: https://jsonplaceholder.typicode.com/ 但是正如您在这里看到的那样,这似乎是获取那些json数据的方法: 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: 例如,这是我在Safari 9上遇到的错误:

在此处输入图片说明

I tried some solutions but now I get this error: 我尝试了一些解决方案,但是现在出现此错误:

在此处输入图片说明

posts are not printed yet, any idea? 帖子尚未打印,有什么想法吗?

Just use normal function syntax instead of ES6 arrow-syntax: 只需使用常规函数语法而不是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();

Most browsers that don't support ES6 arrow-syntax are unlikely to support the Fetch API. 大多数不支持ES6箭头语法的浏览器不太可能支持Fetch API。 For those I would suggest using another form of HTTP request, or using a Polyfill, like GitHub's 对于那些我建议使用其他形式的HTTP请求或使用Polyfill,例如GitHub的

Use a normal function instead of a lambda: 使用普通函数代替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 : 另外,如果您不确定脚本中的ES6语法,则可以使用类似babel的方法

https://babeljs.io/repl/ https://babeljs.io/repl/

you need use fetch polyfill and old function syntax not the new arrow function syntax of es6. 您需要使用访存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> 

Browser Support For Fetch API polyfill 浏览器对Fetch API polyfill的支持

  • Chrome
  • Firefox 火狐浏览器
  • Safari 6.1+ Safari 6.1+
  • Internet Explorer 10+ Internet Explorer 10+

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM