使用不同的API参数更新DIV内容

Update DIV content with different API parameters

本文关键字:参数 更新 DIV 内容 API      更新时间:2023-12-30

我正在尝试在加载DOM后更新div的内容。

内容是从API加载的,该API根据传递的参数值加载不同的内容。

内容加载良好。但是,我有一个选项,用户可以只加载"Sales"或"Rentals"属性,所以我有了一个onClick函数来更新API的参数。

这是我的代码

var url = "http://api.zoopla.co.uk/api/v1/property_listings.js";
var key = "APIKEY";
var branch = "7103";
var ajaxURL = url + "?branch_id=" + branch + "&api_key=" + key;
$('#rentals a').click(function (evt) {
    var link = $(this).attr('href');
    ajaxURL += link;
    $("#main").load('#main');
    evt.preventDefault();
});
$.ajax({
    type: 'GET',
    dataType: 'jsonp',
    jsonp: 'jsonp',
    url: ajaxURL,
    success: function (data) {
        $.each(data.listing, function (i, property) {
            $("#main").append('<h3>' + property.displayable_address + '</h3><p>' + property.description + '<br /> <img src="' + property.image_url + '" /> </p>')
        });
    },
    error: function () {
        alert("Sorry, I can't get the feed");
    }
}); // end GET

这里是这个代码,我无法让我们工作

$('#rentals a').click(function (evt) {
    var link = $(this).attr('href');
    ajaxURL += link;
    $("#main").load('#main');
    evt.preventDefault();
});

.load()函数期望向它传递一个url,但您传递的是相同的选择器(#main)。它可能应该更像这样:

$('#main').load(ajaxURL);