Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7502237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:47:40+00:00 2026-05-29T20:47:40+00:00

I have this function that checks a UK postcode. The problem is that it

  • 0

I have this function that checks a UK postcode. The problem is that it never returns true or false – only undefined.

function PostcodeAnywhere_Interactive_FindByPostcode_v1_00(Key, Postcode, UserName) {

    var retval;

    $.getJSON("https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/FindByPostcode/v1.00/json3.ws?",
    {
        Key: Key,
        Postcode: Postcode,
        UserName: UserName
    },
    function (response) {
        // Test for an error
        if (response.Items.length == 1 && typeof(response.Items[0].Error) != "undefined") {
            // Show the error message
            retval = false;
        } else {
            // Check if there were any items found
            if (response.Items.length == 0){
                retval = false;
            } else {
                retval = true;
            }
        }
    });

return retval;

}

To me it looks like it should always return true or false, so I can’t understand where I’m going wrong. Can someone please help? Is it that the getJSON function needs time to execute?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:47:43+00:00Added an answer on May 29, 2026 at 8:47 pm

    It returns undefined because $.getJSON runs asynchronously and for this reason retval is returned before the success function of $.getJSON is executed. If you need to use retval you must call the function that uses it in the callback

       $.getJSON("https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/FindByPostcode/v1.00/json3.ws?",
        {
            Key: Key,
            Postcode: Postcode,
            UserName: UserName
        },
        function (response) {
            // Test for an error
            if (response.Items.length == 1 && typeof(response.Items[0].Error) != "undefined") {
                // Show the error message
                retval = false;
            } else {
                // Check if there were any items found
                if (response.Items.length == 0){
                    retval = false;
                } else {
                    retval = true;
                }
                //use retval
                do_something_with_retval(retval);
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code snippet inside a function that checks if an object exists
Inside a function I have created, I have this loop that checks if the
I have this function that checks if a page is the parent: function is_tree($pid)
I have this js function that checks if OK or Cancel is pressed and
My situation is like this: I have a function that returns a value as
This is driving me nuts, I have a login function that checks to make
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
So I have this function that forks N number of child processes. However it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.