简体   繁体   中英

Behance API Ajax load content

I am trying to get the following code to work in order to load the project content into the page on selection of an image. I have the images loading from the behance api, but I am struggling with the second bit when the a is selected it should take the url from the page and add it to the JSON to load each project content into the page. Can anyone see where I am ogin

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script> 
$(document).ready(function(){
var jqxhr;
jqxhr = $.getJSON("http://www.behance.net/v2/users/USER/projects?api_key=KEY&callback=?", function(data) {
    var project_str = "";
    for(var i=0; i<  data.projects.length; i++){
        obj = {};
          obj = data.projects[i];
project_str += '<a class="link" href="#' + obj.id  + '"><img src="' + obj.covers['404'] + '" /></a>';       }
    $('#behance_container div').append(project_str);
});
          });

$('a.link').click(function(){
var hash = location.hash.replace("#","");
var jqxhri;
jqxhri = $.getJSON("http://www.behance.net/v2/projects/' + hash + '?api_key=KEY&callback=?", function(data) {
    $('#behance_header h3').html(data.project.name);
    var project_data = '<p>' + data.project.description + '</p>';       
    $('#behance_project').html(project_data);   });
        });
</script>
</head>
<body>
    <div id='behance_container'>
    <div></div>  
    </div>
    <div id='behance_header'><h3></h3><p></p></div> <div id='behance_project'></div>
</body>
</html>

You are defining getBehanceProject(id) to take an id argument and you are using that argument in the function, but when you call the function, you do not pass an id argument.

Further, in getBehanceProject(id) , you are then defining a local variable called id too which should not be.

The first thing you should fix is to pass the desired id to getBenhanceProject() when you call it.


Secondly, you need to realize that addBehance() is asynchronous. It will finish some time AFTER you call it. So, in your current code, you will be calling getBehanceProject() BEFORE the AJAX call in addBehance() has completed which will probably cause a problem for your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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