Anusha Anusha - 5 months ago 39
JSON Question

Error:In JSON data submission unexpected character

I am trying to send a push notification using onesignal.In that process i got an error

"error":"There was a problem in the JSON you submitted: unexpected character at line 1, column 1 [parse.c:652]"}


My Code is as follows:

var jsonBody = {
"app_id": "OneSignal App ID",
"include_player_ids": ["Onesignal Playerid"],
"headings": {
"en": "Sump"
},
"contents": {
"en": " Sump Level is 'Sumpper' "
}
};
var request = $.ajax({
url: "https://onesignal.com/api/v1/notifications",
headers: {
'Authorization':'Basic REST API Key',
'Content-Type':'application/json',
'Access-Control-Allow-Headers': 'SDK-Version',
'Access-Control-Allow-Origin': '*'
},
type: "POST",
data: jsonBody,
dataType: "json"
});
console.log(request);

request.success(function(msg) {
console.log("success");
});

request.error(function(jqXHR, textStatus ) {
console.log( "Request failed: " + textStatus );

});


Can anyone help me where i am going wrong.

Thankyou.

Answer Source

Hi try to create an object then stringfy it to json.

var jsonBody = 
          { 
           app_id: "OneSignal App ID",
           include_player_ids: [ "Onesignal Playerid", "Secondsignal Playerid"],
           headings : 
           {
              en: "Sump"
           },
           contents :{
             en: "Sump Level is 'Sumpper' "
           }  
         }; 

var request = $.ajax({
                      url: "https://onesignal.com/api/v1/notifications",
                      headers: {
                            'Authorization':'Basic REST API Key',
                            'Content-Type':'application/json',
                            'Access-Control-Allow-Headers': 'SDK-Version',
                            'Access-Control-Allow-Origin': '*'
                        },
                      type: "POST",
                      data: JSON.stringify(jsonBody),
                      dataType: "json"
                });
                console.log(request);

                    request.success(function(msg) {
                      console.log("success");
                    });

                    request.error(function(jqXHR, textStatus ) {
                      console.log( "Request failed: " + textStatus );

                    });