Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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
jquery postcode anywhere api不返回值_Jquery_Api_Return - Fatal编程技术网

jquery postcode anywhere api不返回值

jquery postcode anywhere api不返回值,jquery,api,return,Jquery,Api,Return,我正在使用postcode anywhere查找服务下面的代码允许我控制台记录值,但不会将其作为对象返回 function StoreFinder_Interactive_RetrieveNearest_v1_10(Key, Origin, MaximumItems, MaximumRadius, MaximumTime, DistanceType, LocationLists) { $.getJSON("http://services.postcodeanywhere.co.uk/S

我正在使用postcode anywhere查找服务下面的代码允许我控制台记录值,但不会将其作为对象返回

function StoreFinder_Interactive_RetrieveNearest_v1_10(Key, Origin, MaximumItems, MaximumRadius, MaximumTime, DistanceType, LocationLists) {

    $.getJSON("http://services.postcodeanywhere.co.uk/StoreFinder/Interactive/RetrieveNearest/v1.10/json3.ws?callback=?",
    {
        Key: Key,
        Origin: Origin,
        MaximumItems: MaximumItems,
        MaximumRadius: MaximumRadius,
        MaximumTime: MaximumTime,
        DistanceType: DistanceType,
        LocationLists: LocationLists
    },
    function (data) {
        // Test for an error
        if (data.Items.length == 1 && typeof(data.Items[0].Error) != "undefined") {
            // Show the error message
            alert(data.Items[0].Description);
        }
        else {
            // Check if there were any items found
            if (data.Items.length == 0)
                alert("Sorry, there were no results");
            else {
                // PUT YOUR CODE HERE
                //FYI: The output is a JS object (e.g. data.Items[0].YourId), the keys being:
                distance = data.Items[0].Distance;

                console.log(distance);
               // name = data.Items[0].Name;

                //YourId
                //Name
                //Description
                //Distance
                //Time
                //Easting
                //Northing
                //Latitude
                //Longitude
                return distance;

            }
        }
    });
}
所以我的电话

   var data = StoreFinder_Interactive_RetrieveNearest_v1_10("xxxxxxxxxxxxx", $('#postcode').val() );
console.log("data is"+data)

提供未定义的数据。

这是因为$.getJSON是异步的,在调用下一个函数之前不会立即返回值

因此,在您的示例中:

var data = StoreFinder_Interactive_RetrieveNearest_v1_10("xxxxxxxxxxxxx", $('#postcode').val() );
console.log("data is"+data)
由于StoreFinder函数不会立即返回值,因此代码将转到console.log(),而数据仍未定义

在javascript中处理此问题的惯用方法是在StoreFinder函数的参数中包含回调函数,该函数将在请求完成后执行

下面是一个使用$.getJSON的通用示例:

function StoreFinderFunction(options, callback) {
  $.getJSON(options, function(data) {
    // Do what you need to do when you receive the data
    callback(data);
  });
}
然后记录您的结果:

StoreFinderFunction(my_options, function (data) {
  // This will now log your data 
  console.log("data is"+data);
});