简体   繁体   English

单击超链接后如何触发第二个AJAX调用?

[英]How to trigger a second AJAX call upon clicking of hyperlink?

I've an API that I'm connecting to. 我有一个要连接的API。 I've an index.html page with two columns in it. 我有一个index.html页面,其中有两列。 The left column contains thumbnails of images (see Part A) and the right column will contain details of information about the images once it is clicked on. 左列包含图像的缩略图(请参阅A部分),右列包含单击图像后有关图像的信息的详细信息。 There are currently two AJAX calls (both calling different URLs) within the same JS script file. 当前,同一JS脚本文件中有两个AJAX调用(都调用不同的URL)。

Part A 甲部

$.ajax({
    url: 'http://gateway.marvel.com:80/v1/public/characters?limit=5&offset=300&apikey=' + publickey + "&ts=" + ts + "&hash=" + hash,
    method: 'get',
    success: function (res) {
        var characters = res.data.results;
        var index = 0;

        loadCharacters();

        function loadCharacters() {
            if (index > characters.length) {
                index = 0;
                return;
            }

            for (index = 0; index < characters.length; index++) {
                var hero = characters[index];
                var heroID = hero.id;
                var heroName = hero.name;
                var image = hero.thumbnail;
                var image_url = image.path + '.' + image.extension;

                var image_target = $("#comics")

                $('<img>').attr({
                    src: image_url,
                    width: 80,
                    height: 80,
                }).appendTo($('<a>').attr({
                    href: '#?' + heroID,
                    //}).click(dummyCall(hero.id)).appendTo(image_target)); //NOTE: to figure how to pass this hero.id to second ajax call..
                }).appendTo(image_target)); 

                image_target.append("<br/>" + heroName + "<br/>");
            }
        } // end first ajax
    }
});

Part B B部分

$.ajax({
    url: "https://gateway.marvel.com:443/v1/public/characters/1009269/comics?limit=5&apikey=" + publickey + "&hash=" + hash + "&ts=" + ts,
    method: 'get',
    success: function (res) {
        var comicDetails = res.data.results;
        var index = 0;

        loadComic();

        function loadComic() {
            if (index > comicDetails.length) {
                index = 0;
                return;
            }
            for (index = 0; index < comicDetails.length; index++) {
                var comic = comicDetails[index];
                var comicTitle = comic.title;
                $("#comics").append("<br/>" + comicTitle + "<br/>");
            }
        }
    }
});

Part B is still incomplete, but for the purpose of this question, it should suffice. B部分仍不完整,但出于这个问题的目的,它就足够了。 I need to know how I can trigger Part B once the user clicks on the anchored image hyperlinks in the left sidebar. 我需要知道一旦用户单击左侧边栏中的定位图像超链接,如何触发B部分。 I don't want Part B to load immediately because Part B is dependent on the hero.id (from Part A) being clicked and passed to it. 我不希望B部分立即加载,因为B部分取决于被点击并传递给它的hero.id (来自A部分)。

You'll want to have a click handler for the images. 您将需要图像的点击处理程序。 You can do this in a couple ways. 您可以通过几种方式执行此操作。

Either directly 要么直接

$imageElements.on('click', functionThatCallsAjaxB);

Or indirectly with a delegate 或间接与代表

$("#comics").on('click', 'img', functionThatCallsAjaxB);

The difference being that the first puts a handler on each image and the image has to exist when you execute that line. 区别在于,第一个在每个图像上放置一个处理程序,并且在执行该行时该图像必须存在。 The second is a single handler attached to the parent that reacts when one of this children has that event. 第二个是附加到父级的单个处理程序,当该子级中的一个发生该事件时作出反应。

I also see in your ajax A that your wondering how to pass information to the second ajax call. 我还在您的Ajax A中看到您想知道如何将信息传递给第二个Ajax调用。 One way you can do this is with data elements. 做到这一点的一种方法是使用数据元素。

$element.data('heroId', 'myvalue'); <-- setter
$element.data('heroId') <-- getter

So you can set them on the images when you create them and reference them later. 因此,您可以在创建图像时将其设置在图像上,并在以后引用它们。

An assumption on my part, lets assume that hero id is a unique value for each image. 就我而言,假设每个图片的英雄ID都是唯一的值。 If so, in you loop, 如果是这样,在您循环中,

          $('<img>').attr({
                src: image_url,
                width: 80,
                height: 80,
                id: hero.id,

                ...

Because you are assigning a click handler, you don't need the '< a>'. 因为您要分配点击处理程序,所以不需要'<a>'。

Elaborating on Taplar's answer a little more, $("#comics").on('click', 'img', functionThatCallsAjaxB); 详细说明Taplar的答案,$(“#comics”)。on('click','img',functionThatCallsAjaxB); after the looping is done (you only have to do it once. If you do it this one... 循环完成后(您只需要执行一次即可。如果执行此操作...

$("#comics").on('click', 'img', function(evtObj){ functionThatCallsAjaxB(evtObj);});

with function 具有功能

 functionThatCallsAjaxB(obj) {
     // the image that was clicked will be obj.target.
     // with the above assumption, 
     var heroID = obj.target.id;

     // the rest of your part b ajax goes here.
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM