2016-08-20 87 views
3

我试图用取发送POST请求OneSignal REST API:反应本地取“不支持BodyInit型”

var obj = { 
    method: 'POST', 
    headers: { 
    'Accept': 'application/json', 
    'Content-Type': 'application/json', 
    }, 
    body: JSON.stringify({ 
    'app_id': '(API KEY)', 
    'contents': {"en": "English Message"}, 
    'app_ids': ["APP IDS"], 
    'data': {'foo': 'bar'} 
    }) 
} 

fetch('https://onesignal.com/api/v1/notifications', obj) 

我知道你不是真的应该把你的API密钥客户端代码,但这只是一个测试,看看它是否会起作用。另外,我发现了错误不是来自服务器的响应不好,它是:

Possible Unhandled Promise Rejection (id: 0): 
unsupported BodyInit type 

我试图把一个catch方法上取,但它不会被调用。

有点遗憾,不确定如何继续。

在此先感谢!

回答

0

你试过把你的json改成下面的那个吗?

JSON.stringify({ 
    app_id: '(API KEY)', 
    contents: {en: "English Message"}, 
    app_ids: ["APP IDS"], 
    data: {foo: 'bar'} 
}) 

甚至尝试过更简单的json?

1

即使我尝试了用于创建通知的One-Signal REST API的相同POST请求,下面的工作对我来说很好。

const bodyObj = { 
    app_id: "**********", 
    included_segments: ["All"], 
    data: {"foo": "bar"}, 
    contents: {"en": "Hi good morning"} 
} 

    fetch('https://onesignal.com/api/v1/notifications',{ 
    method:'POST', 
    headers:{ 
    'Authorization':'Basic **********', 
    'Content-Type':'application/json' 
    }, 
    body:JSON.stringify(bodyObj) 
}) 
    .then((response) => response.json()) 
    .then((responseJson) => { 
    console.log("success api call"); 
    }) 
    .catch((error) => { 
     console.error(error); 
    });