最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "JSON_PARSING_ERROR: Unexpected character (d) at position 0." when sending GCM request from GAS -

programmeradmin2浏览0评论

This seems very similar to a number of other questions and it seems obvious that the error indicates there's something wrong with my JSON payload. But I'm at a loss as to why.

I'm running a Google Apps Script to test sending a message to Google Firebase Cloud Messaging.

My code:

function SendGCMessage() {
  var url = "";
  var apiKey = "AbCdEfG";
  var to = "ZyXwVuT:ToKeNtOkEnToKeNtOkEnToKeNtOkEn"
  var payload = {
    "data": {
      "message" : "This is the message"
    },
    "to":to
  };

  var sendCount = 1;
    var headers = {
      "Content-Type": "application/json",
      "Authorization": "key=" + apiKey
    };
    var params = {
      headers: headers,
      method: "post",
      payload: payload
    };

    var response = UrlFetchApp.fetch(url, params);
  return {message: "send pleted: " + response.getContentText()};
}

When I run this in debug mode, the object payload looks fine - like a normal Javascript object. params as well. UrlFetchApp takes a Javascript object, not a String in JSON notation. However I did try "JSON.stringify(params)" and I got an error. What did I do wrong?

Note: params looks like this when I pause it in the debugger:

{"headers":{"Content-Type":"application/json","Authorization":"key=AbCdEfG"},"method":"post","payload":{"data":{"message":"This is the message"},"to":"ZyXwVuT:ToKeNtOkEnToKeNtOkEnToKeNtOkEn"}}

This seems very similar to a number of other questions and it seems obvious that the error indicates there's something wrong with my JSON payload. But I'm at a loss as to why.

I'm running a Google Apps Script to test sending a message to Google Firebase Cloud Messaging.

My code:

function SendGCMessage() {
  var url = "https://gcm-http.googleapis./gcm/send";
  var apiKey = "AbCdEfG";
  var to = "ZyXwVuT:ToKeNtOkEnToKeNtOkEnToKeNtOkEn"
  var payload = {
    "data": {
      "message" : "This is the message"
    },
    "to":to
  };

  var sendCount = 1;
    var headers = {
      "Content-Type": "application/json",
      "Authorization": "key=" + apiKey
    };
    var params = {
      headers: headers,
      method: "post",
      payload: payload
    };

    var response = UrlFetchApp.fetch(url, params);
  return {message: "send pleted: " + response.getContentText()};
}

When I run this in debug mode, the object payload looks fine - like a normal Javascript object. params as well. UrlFetchApp takes a Javascript object, not a String in JSON notation. However I did try "JSON.stringify(params)" and I got an error. What did I do wrong?

Note: params looks like this when I pause it in the debugger:

{"headers":{"Content-Type":"application/json","Authorization":"key=AbCdEfG"},"method":"post","payload":{"data":{"message":"This is the message"},"to":"ZyXwVuT:ToKeNtOkEnToKeNtOkEnToKeNtOkEn"}}

Share Improve this question edited Nov 5, 2016 at 15:16 Scott asked Apr 10, 2016 at 0:10 ScottScott 3,7529 gold badges35 silver badges58 bronze badges 2
  • Have you tried building payload like a js object literal? – Tyr Commented Apr 10, 2016 at 0:12
  • Is that not what I did? – Scott Commented Apr 10, 2016 at 0:17
Add a ment  | 

1 Answer 1

Reset to default 8

I discovered the problem, thanks to https://stackoverflow./a/10894233/3576831

the 'payload' parameter must be a string as specified here: https://developers.google./apps-script/class_urlfetchapp?hl=fr-FR#fetch.

Adjusting this section of the script works:

var params = {
  headers: headers,
  method: "post",
  payload: JSON.stringify(payload)
};

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论