Jquery 如何使用一个API的结果来在下面的/嵌套的API中使用?

Jquery 如何使用一个API的结果来在下面的/嵌套的API中使用?,jquery,api,Jquery,Api,我正在使用jQuery使用MapQuest API(地理编码+请搜索端点)。目标是存储从第一个API中的用户输入填充的lat/long值,并将这些值传递到“circle”参数中的下一个API中。完整代码如下: codeBreakApp.searchResults = (location, query) => { $.ajax({ url: codeBreakApp.searchEndPoint, method: 'GET', dataType: '

我正在使用jQuery使用MapQuest API(地理编码+请搜索端点)。目标是存储从第一个API中的用户输入填充的lat/long值,并将这些值传递到“circle”参数中的下一个API中。完整代码如下:

codeBreakApp.searchResults = (location, query) => {
   $.ajax({
      url: codeBreakApp.searchEndPoint,
      method: 'GET',
      dataType: 'JSON',
      data: {
         key: codeBreakApp.apiKey,
         location: location,
      },
   }).then((res) => {
      console.log(res);
      console.log(res.results[0].locations[0].latLng);
      
      let locationLng = (res.results[0].locations[0].latLng.lng);
      let locationLat = (res.results[0].locations[0].latLng.lat);
      
      console.log(locationLng);
      console.log(locationLat);
      
      $.ajax({
         url: codeBreakApp.placeEndPoint,
         method: 'GET',
         dataType: 'JSON',
         data: {
            key: codeBreakApp.apiKey,
            circle: [locationLng, locationLat, 5000],
            q: query,
            sort: 'relevance',
            pageSize: 10,
         },
      }).then((res) => {
         console.log(res);
         console.log('Found something');

      }).fail((err) => {
         console.log(err);
         console.log('Failed');
      });
   });
}

const userSearch = function() {
   $('.form').on('submit', function(e) {
      e.preventDefault();

      // Start location input
      let userLocationInput = $('input').val();
      console.log(userLocationInput);

      // Places input
      let selectedPlaceInput = $('#places option:selected').text();
      console.log(selectedPlaceInput);


      codeBreakApp.searchResults(userLocationInput, selectedPlaceInput);
   });
}

// Initializing functions
codeBreakApp.init = function () {
   codeBreakApp.searchResults((''), (''));
   userSearch();
}

// Document ready
$(function () {
   codeBreakApp.init();
});
我的控制台日志返回我从表单输入值中查找的信息,并且我能够从第一个API结果对象中检索lat/long,但是当我运行第二个API时,什么也没有发生。有什么办法可以解决这个问题吗


谢谢

第二个api应该像承诺中的那个样运行——第一个api完成后,它应该立即运行。运行第二个API是什么意思?第二个API不会在console.log(res)或console.log(err)中返回响应。我认为这与我获取locationLat和locationLng变量(存储第一个API结果中的lat+长整数)的方式有关,并将其作为第二个API的“圆圈”中的值包含在内。不管它是否返回locationLat。如果缺少locationLat,ajax将运行并抛出错误。我想说的是,只要第一个console.log(res)显示,它就应该运行。我设法得到了以下错误,它似乎与“circle”参数直接相关。我甚至试着加入一些静态长/宽坐标和无骰子!已阻止跨源请求:同一源策略不允许读取位于的远程资源。(原因:CORS标题“访问控制允许来源”丢失)。太好了!看起来第二个API调用中的API端点有CORS问题-顺便说一句,当我单击该
mapquestapi
时,我可以在浏览器中看到一些数据。