Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript函数未返回正确的值_Javascript_Jquery_Function - Fatal编程技术网

javascript函数未返回正确的值

javascript函数未返回正确的值,javascript,jquery,function,Javascript,Jquery,Function,我有这个功能,检查英国邮政编码。问题是它从不返回true或false——只返回未定义的 function PostcodeAnywhere_Interactive_FindByPostcode_v1_00(Key, Postcode, UserName) { var retval; $.getJSON("https://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/FindByPostcode/v1.

我有这个功能,检查英国邮政编码。问题是它从不返回
true
false
——只返回
未定义的

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;

}

在我看来,它应该总是返回
true
false
,所以我不知道哪里出错了。有人能帮忙吗?getJSON函数是否需要时间来执行?

它返回未定义,因为
$。getJSON
异步运行,因此在执行
$.getJSON
的成功函数之前返回retval。如果需要使用
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;
            }
            //use retval
            do_something_with_retval(retval);
        }
    });

它返回undefined,因为
$.getJSON
异步运行,因此在执行
$.getJSON
的success函数之前返回retval。如果需要使用
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;
            }
            //use retval
            do_something_with_retval(retval);
        }
    });

可能重复--请在提问前使用搜索。其他人是否阅读了Nick Craver关于SO存储要求的博客文章?我想知道像这样的问题占用了多少磁盘阵列空间?:-)只是因为您在设置retval之前返回了它(异步)很抱歉大家都发布了一个问题-我没有意识到
$.getJSON
的工作方式与
$.ajax
类似,因此它是异步的,可能重复的,请在提问之前使用搜索。还有人读过Nick Craver关于存储的博客文章吗要求?我想知道像这样的问题占用了多少磁盘阵列空间?:-)只是因为您在设置之前返回了retval(异步),很抱歉每个人都发布了一个已经问过的问题-我没有意识到
$.getJSON
的工作方式与
$.ajax
类似,因此它是异步的,可能与