In event javascript doesn't seem to work with JSON Stringify.

0 favourites
  • 6 posts
  • Not sure what I'm doing wrong here?

    I have a dictionary Score_D where I store a bunch of score data and on end of a level, call this block to put the score through an ajax call. I couldn't get Json.Stringify and so I figured, I'd get a local variable that has the json converted to a string, stringify it again with the json.stringify to escape the quotes. This works fine when I preview and in debug mode. When I export to a html5 this stops working. Oddly the console.log does not show as a scriptsinevents.js message, it shows as a c3runtime.js.

    I simply need to escape the quotes before making the ajax call! I've been going around in circles with this. Any help is appreciated.

  • I'm really struggling to understand what's going on in your screenshot. Have you tried replacing double quotes " with single ' before sending?

    Set s to replace(Dictionary.AsJSON, """", "'")

    .

    Also, it's annoying to keep track of all these quotation marks in expressions and very easy to make a mistake. So I prefer this method of building JSON strings:

    .

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You don't need to escape anything from the JSON_Put object in this case. You're just passing strings around.

    The JSON ToBeautifiedString expression already stringifies the JSON. You shouldn't try to stringify it a second time.

  • Hey Ashley,

    Thanks for the explanation. Since the dictionary itself has the actual data inside of the data key, basically a JSON inside a json, I'm having to stringify twice.

    Beautified String gives me this:

    which when directly passed as the data in the api fails.

    On stringifying again, I get this which seems to work:

    .

    Instead of using localvars, I'm now using globalvars which seems to work even in the export. Would you recommend instead that I just pass the data value to the API and reconstruct the dictionary after retrieving it in a get call?

  • Again, ToBeautifiedString already calls JSON.Stringify internally, and then you are still calling JSON.Stringify a second time via scripting. It's definitely wrong to stringify twice, you should only do it once, and if that doesn't work for some reason, figure out why and solve that problem instead.

  • Ah realized my error! I was beautifying the inner json but not setting the actual put json string correctly. Both Ashley and dop2000 were right. In case someone stumbles upon this, this is what I did to get it to work - beautify the "nesting" json and then add it to the json I was going to use in the ajax call. Then beautified the json and used it in the ajax call. This works now for me without scripting. Thanks for the help.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)