Py学习  »  Jquery

如何使用jquery合并两个不同的url进行解析

progy85 • 4 年前 • 342 次点击  

我想尝试使用两个不同的url进行解析,但我的代码不起作用:

   var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";

$.when($.getJSON(url), $.getJSON(url2)).done(function (data1, data2) {

    alert(data1.address.village);
    alert(data2.address.city);

    //do stuff with 'data' and 'data2'
});


$.getJSON("https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311", function (data) {
    $.getJSON("https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311", function (data2) {
        var concatenatedJson = $.extend({}, data, data2);

        alert(concatenatedJson.address.road);

    });
});
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54748
 
342 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Mourad Karoudi
Reply   •   1 楼
Mourad Karoudi    4 年前
var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";


var data = {};

$.getJSON(url, function(result){
  data.data1 = result;
});

$.getJSON(url2, function(result){
  data.data2 = result;
});


console.log(data); // will return the two urls json code
balexandre
Reply   •   2 楼
balexandre    4 年前

你唯一的错就是 假设

简单的 console.log(data1) 就足以告诉你 data1 不是来自URL的数据,而是包含更多信息的数组

enter image description here

var url = "https://nominatim.openstreetmap.org/reverse.php?zoom=15&format=json&lat=45.9660047&lon=13.6408311";
var url2 = "https://nominatim.openstreetmap.org/reverse.php?zoom=18&format=json&lat=45.9660047&lon=13.6408311";

$.when($.getJSON(url), $.getJSON(url2)).done(function (data1, data2) {

    alert(data1[0].address.village);
    alert(data2[0].address.city);

    //do stuff with 'data' and 'data2'
});