0

I am using javascript and jquery to send a simple text push notification via parse.com using their rest api. This works :-

$.ajax({
    type: 'POST',
    headers: {
        'X-Parse-Application-Id': "1234567890",
        'X-Parse-REST-API-Key': "1234567890"
    },
    url: "https://api.parse.com/1/push",
    data: '{"channel": "","type":"ios","expiration_interval":86400,"data":{"alert":"canned alert","badge" :0,"sound":""}}',
    contentType: "application/json"
});

But this does not:-

 var my_msg = "canned alert";
 $.ajax({
     type: 'POST',
     headers: {
         'X-Parse-Application-Id': "1234567890",
         'X-Parse-REST-API-Key': "1234567890"
     },
     url: "https://api.parse.com/1/push",
     data: '{"channel": "","type":"ios","expiration_interval":86400,"data":{"alert":my_msg,"badge" :0,"sound":""}}',
     contentType: "application/json"
 });

I cannot find a way to replace successfully send an alert with a variable instead of "canned alert". I am not an experienced programmer but even the parse.com tesch support could not explain why; can anyone suggest a solution please?

1
  • Can you elaborate on what "does not work" means in this context? Did you get an error response back, or was it a successful request but no push notifications were delivered? Commented Mar 18, 2013 at 21:26

3 Answers 3

2

Take the my_msg variable out of the single quotes in the second snippet, and it should behave exactly like the first one:

data: '{"channel": "","type":"ios","expiration_interval":86400,"data":{"alert":'+my_msg+',"badge" :0,"sound":""}}',

The way you wrote the second snippet, it looks like you confused a JSON string with an object literal.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you flesh out your answer, please?
0

You can use JSON.stringify to build the JSON from a plain object:

var my_msg = "canned alert";
$.ajax({
  type: 'POST',
  headers: {'X-Parse-Application-Id':"1234567890",'X-Parse-REST-API-Key':"1234567890"},
  url: "https://api.parse.com/1/push",
  data: JSON.stringify({
    "channel": "",
    "type":"ios",
    "expiration_interval": 86400,
    "data":{
      "alert": my_msg,
      "badge" :0,
      "sound":""
    }
  }),
  contentType: "application/json"
});

Using JSON.stringify will ensure that any special characters in "my_msg" will be correctly escaped, so that your JSON is guaranteed to be valid.

Comments

0

You can pass data as an object, no need of any quotes, jQuery will take care of it:

var my_msg = "canned alert";
$.ajax({
    type: 'POST',
    headers: {
        'X-Parse-Application-Id': "1234567890",
            'X-Parse-REST-API-Key': "1234567890"
    },
    url: "https://api.parse.com/1/push",
    data: {
        channel: "",
        type: "ios",
        expiration_interval: 86400,
        data: {
            alert: my_msg,
            badge: 0,
            sound: ""
        }
    },
    contentType: "application/json"
});

3 Comments

Are you sure that that API correctly deals interprets HTTP parameters? (It might; I don't know for sure.)
I have always used it this way. I'm not sure if it were JSONP though, but POST should be ok. Documentation says The data option can contain either a query string of the form key1=value1&key2=value2, or an object of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string
Yes, I know that jQuery itself works like that - what I don't know is whether the API that's being called (parse.com?) wants a JSON blob or name/value HTTP parameters. (Note that the OP says that sending a complete JSON string that's not dynamic did work.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.