1

Newbie to javascript/jquery/ajax...

Here is the ajax call:

$.ajax({
    type: "POST",
    url: "cfc/newsletter.cfc?method=sendResource",
    data: $this.serialize(),
    success: function(data) {
        console.log(data);
        if (data.SUCCESS == 'true') {
          message.addClass('opened');
          text = "Your email has been sent successfully!";
          message.html('<div class="alert_box r_corners color_green success"><p>' + text + '</p></div>')
            .slideDown()
            .delay(4000)
            .slideUp(function() {
              $(this).html("");
              message.removeClass('opened');
            })
            .prevAll('input[type="email"]').val("");
        } else {

in the console, here is what is logged as the data parameter...

<wddxPacket version='1.0'><header/><data><struct><var name='SUCCESS'><string>true</string></var></struct></data></wddxPacket>

So, how do I access the SUCCESS variable so that my else block does not fire?

data.SUCCESS == true and data.SUCCESS == 'true' don't work (nor any other thing I have tried).

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Mark
  • 13
  • 5
  • *"I can't even seem to get the question formatted correctly...not a good sign..."* There is a help box to the right of the question input, buttons that help you format the content and a link to https://stackoverflow.com/editing-help. – Felix Kling Mar 25 '16 at 19:53
  • 1
    It seems you get XML back. Read in the jQuery [documentation](https://api.jquery.com/jQuery.ajax/) about the `dataType` option and use the Stack Overflow search: [`[jquery] ajax xml`](https://stackoverflow.com/search?q=%5Bjquery%5D+ajax+xml). You are likely not the first person who needs to access XML in a jQuery ajax response callback. – Felix Kling Mar 25 '16 at 19:57
  • Duplicate: [Coldfusion jQuery getJSON : Getting WDDX instead of JSON](http://stackoverflow.com/questions/4777089/coldfusion-jquery-getjson-getting-wddx-instead-of-json) – Yogi Mar 25 '16 at 20:16
  • I did see that post above (which didn't seem to help), and I can specify JSON as return type, that give me this in the console: {"SUCCESS":true} I am logging out the data returned to try and figure out how to access it correctly so that the if statement triggers true...can't seem to get it done. With the JSON returned like so...{"SUCCESS":true}, putting data.SUCCESS = 'true' or data.SUCCESS = true, both fail. leaving out the data part causes, as one would expect, an undefined variable error. So, any clues would be helpful. I hope I am explaining myself correctly. – Mark Mar 25 '16 at 20:39
  • `if (data.SUCCESS)` would be the correct use, assuming `data` is an object. If not you need to parse the JSON first, either explicitly or let jQuery do it for you. Here again, looking at the `dataType` option of the jQuery ajax call will help. – Felix Kling Mar 25 '16 at 20:44
  • Trying to state that comment better: I did see that post above, labeled Duplicate (which didn't seem to help), and I can specify JSON as return type, that give me this in the console: {"SUCCESS":true} I am logging out the data returned to figure out how to access it correctly so the if statement in the success function triggers true... With the JSON returned like so...{"SUCCESS":true}, putting data.SUCCESS = 'true' or data.SUCCESS = true, in the if statement above, fails. All I am trying to do at this point, is get the if statement to be true... – Mark Mar 25 '16 at 20:46
  • Let me repeat my comment as well: *"If not you need to parse the JSON first, either explicitly or let jQuery do it for you."* If the output in the console is `{"SUCCESS":true}`, then `data` is a **string** containing JSON. You have to **parse** the JSON first, either explicitly using `JSON.parse` or by letting jQuery do it for you with `dataType: 'json'`. See http://stackoverflow.com/q/45015/218196 . If you are not familiar with JSON in JavaScript, read about it first. – Felix Kling Mar 25 '16 at 20:56
  • Sorry to be so dense...changed the console.log to: console.log(JSON.parse(data)); which yields: Object {SUCCESS: true} in the console. So I change the if statement to: if(JSON.parse(data).SUCCESS == true), but the if is not triggering with or without quotes around true. Just skips right to the else...Thanks for your help. I'm old and it is much harder to learn new things (at least that is what I tell myself to feel better about being so dense). – Mark Mar 25 '16 at 21:30
  • So, I added this above the if statement: var testit = JSON.parse(data).SUCCESS; and then: console.log(testit); And it testit logs as: true in console...so I don't understand why it won't work as above...going to try if(testit == true) and see if that will work. – Mark Mar 25 '16 at 21:55
  • Well...setting a variable to JSON.parse(data).SUCCESS and then testing if that variable is true, did work. Seems pretty hacky, but I'll take it. Thanks for your help. – Mark Mar 25 '16 at 21:58

0 Answers0