2017-05-04 46 views
1

我想解析服務工人內部的json,但我無法做到這一點。解析json服務工人越來越undefined

這是我想要的推送通知解析數據:

{"data": {"url": "www.google.com", "text": "teste", "icon": "http://example.com/abc.gif", "title": "title"}} 

這是我的代碼:

self.addEventListener('push', function(event) { 

    var payload = event.data.text(); 


    if (payload.length > 0) { 

     event.waitUntil( 
      fetch(payload, { 
      method: 'get' 
      }) 
      .then(function(response) { 
       console.log('response'); 
       console.log(response); 
       return response.json(); 
      }) 
      .then(function(data) { 
       console.log(data.text); 
       console.log(data.title); 
       self.registration.showNotification(data.title, { 
        body: data.text, 
        icon: data.icon 
       }); 
      }) 
      .catch(function(err) { 
      console.log('err'); 
      console.log(err); 
      }); 

     ); 

    } 

我越來越data.text和data.icon未定義。

我該如何解決這個問題?

回答

0

使用JSON.parse(有效載荷)來分析有效載荷