1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

getJSON-return value

Discussion in 'jQuery' started by PoPSiCLe, Nov 14, 2013.

  1. #1
    Okay, I've read through the API docs, and can't really get this to work properly. I have a function, which takes arguments, and I want to use one of those to fetch a value from a return JSON-array.

    I have the following code:
    
    $.fn.positionMessages = function(element, getMessage, required) {
    
         $text = $.getJSON("returnmessages.php", function(data) {
           console.log(data.requiredField);
           console.log(data.getMessage);
           return data.requiredField;
         });
    
    Code (markup):
    The problem is as follows:
    The first console.log(data.requiredField) returns the value.
    The second console.log(data.getMessage) returns undefined;
    and the return data.requiredField also returns undefined (when calling $text).

    So, what am I supposed to do to get that message into the $text-variable?

    The JSON returns correctly, it's the parsing of the return values that seems to fail
     
    PoPSiCLe, Nov 14, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    can we see a sample response from that PHP? Could be you have a incorrectly formed JSON structure that jQ's parser is failing silently. (another reason I don't trust JQ in production environments, much like HTML not enough "throw" and way too much "plod on like nothing is wrong")

    Have you tried checking the returned result using a flat non-jq request, dumping it into a PRE to make sure it's getting through intact/properly formed? Is it possible in your JSON structure

    Really though, this is why I don't use JSON (or XML for that matter), and instead either run comma delimited or ASCII fieldstructures instead... well on top of them being leaner/easier to parse even if you don't get nice neat object names out of it. I realize I'm probably one of the last people on the planet still using ASCII-Structs
     
    deathshadow, Nov 15, 2013 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    The return values are as follows:
    from FireBug:
    JSON value
    
    requiredField "Dette feltet må fylles ut"
    passwordLength "Passordet må bestå av minst 8 tegn"
    
    Code (markup):
    And from response:
    
    {"requiredField":"Dette feltet m\u00e5 fylles ut","passwordLength":"Passordet m\u00e5 best\u00e5 av minst 8 tegn"}
    
    Code (markup):
    And this is the complete PHP file (for now):
    
    <?php
    
    $returnmessages = array(
               "requiredField"=>"Dette feltet må fylles ut",
               "passwordLength"=>"Passordet må bestå av minst 8 tegn"
               );
    
    echo json_encode($returnmessages);
    
    ?>
    
    PHP:
    And, as I said - the console.log works, but only if I spell out the 'requiredField' name - not if I try to use it through a declared variable, and when I try to return something to fill the variable $text, nothing happens, it seems.
     
    PoPSiCLe, Nov 15, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    You have no "getMessage" in your JSON... so it doesn't exist as a property of "data"... did you mean to alert(getMessage) (as passed to the function) instead of alert(data.getMessage)?

    Being a handler it would still cascade despite the fatal error -- so the 'return' never executes despite it actually 'returning'... The complaint about the nonexistent property should be in the error log... Even more likely jQ is possibly hiding that error on you.

    But yeah, it makes things easier, faster and simpler... right.
     
    deathshadow, Nov 15, 2013 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    hm... is it a way to get 'data' to understand that I'm using a variable to fetch whatever value that variable contains?
    Ie, the getMessage is containing the requiredField in this instance - but yes, I understand what happens, I'm just wondering if there's a way to get the JSON interpreter to understand it's not an actual value, but a variable - something akin to PHPs ${$something} to create variable variable-names (yes, I know it's fugly).
    However, right now I'm trying to do a return data.requiredField - that doesn't actually return anything either, even though console.log(data.requiredField) returns the value to the console without any trouble at all.
     
    PoPSiCLe, Nov 15, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Not certain I follow your question... I'm thinking it might help if I had a 'wider' view of what this is even trying to do. I get that it's AJAX pulling JSON from somewhere, but what you are trying to do with the data is a bit confusing. Looks like your taking something simple and making it more complex than need be... would there be a JSON that has that value or no? Are you trying to pass values from the parent function to the handler? Are you mistaking the handler return for the parent it's declared in's return?

    Remember, when you call getJSON it's still a XMLHTTPRequest, meaning it will do the send() and the function you pass will not execute until stage 4 on a 200... meaning the wrapping function will just keep executing.
     
    deathshadow, Nov 15, 2013 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Yup - figured it out. getJSON-call is async, which of course means that the value I was trying to push further into the function wasn't set. Restructured, and now it works (had to wrap the output within the JSON-function. Also figured that data[getMessage] within the JSON-function pulls the correct value from the return data (of course I should have thought of that earlier).

    JSON isn't really my favourite either, but when it's what's being returned, one has to try to work with it :)
     
    PoPSiCLe, Nov 15, 2013 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    It's often easy to forget a lot of commands boil down to being http-requests, and as such are stuck waiting for end of script execution to run.... the incorrectly named "asynchronous execution" -- or as we used to call it, single threaded event driven cooperative non-interrupting multitasking. hmm... I guess Async is easier to say :D

    Both STAR and the original MacOS actually worked the same way -- you could trigger 'events' but they went on a stack, and didn't actually run until the current task ended. (and if it took too long to end or the event stack overflowed, that's when you "got the bomb").

    I feel ya on that, sometimes you're stuck with what the data is -- generally it's why I don't like biting off projects where I don't have 100% control... especially with some of the grossly inefficient storage methods like JSON and XML that have become sick and trendy the past few years.
     
    deathshadow, Nov 15, 2013 IP