0

Here is my code:

function OnSuccess(response) {
        var tok = response.access_token;
        alert(tok);
        $.ajax({
           type: "POST", 
           url: "https://webservice.com/apps/purchases/mass_create",
           data: '{ utoken:' + tok + ',
                    platform: "general",
                    email:"test@gmail.com"
                  }',
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function(response) {
               alert("Success 2");
           },
           failure: function(response) {
                alert("failure 2");
           }

       });
 }

When I run this,while commenting out the entire $.ajax section, I get an alert with the value of tok, but if I leave in the $.ajax, I get an Illegal Token (in Chrome) on the "data: '{ utoken:' + tok + '," line.

What is my mistake?

Thanks

Yoni
  • 316
  • 2
  • 13

2 Answers2

2

New lines characters are not allowed in JavaScript string literals so it errors between the , and the platform.

Even if you didn't have that problem, what are you generating is a very long way from JSON. Stop trying to generate it by hand. Construct a JavaScript object and pass it through JSON.stringify.

data: JSON.stringify({
    utoken: tok,
    platform: "general",
    email:"test@gmail.com"
}),
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I think you can use + instead of , to separate lines in strings. Also you should add ' at the begining and end of each line.

data: '{ utoken:' + tok + ',' +
      'platform: "general",' +
      'email:"test@gmail.com"' +
      '}',
sergiomse
  • 775
  • 12
  • 18