简体   繁体   中英

how to reuse jsonp response variable in jsonp url each time i call ajax via button?

i calling a jsonp functin and get response back but i want to use MaxTagId(var MaxTagId = data.pagination.next_max_tag_id;) the next time i press the button. My code now cant use the value MaxTagId each time i press button since my response from instagram api is same each time. If it it has used the MaxTagId value then each time i was clicking the button i should have got new respone since jsonp url was changing on each call! could any one tell me what i am doing wrong in using MaxTagID in jsonp url each time i press button?

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>


<script>
function callApi2(){
$.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: false,
        url: "https://api.instagram.com/v1/tags/mango/media/recent?access_token=xxxxxxx&max_tag_id=+MaxTagId+",
        success: function(data) {

            alert("Next Max Tag Id:"+data.pagination.next_max_tag_id);
            document.myform.outputtext.value = document.myform.outputtext.value+data.pagination.next_max_tag_id+'\n' ;
            var MaxTagId = data.pagination.next_max_tag_id;

            console.log(data);

    for (var i = 0; i < 21; i++) {

        $(".content").append("<img class='SC-instagram-image' src='" + data.data[i].images.low_resolution.url +"' />");   


        }


        }
    });

}
</script>


</head>
<body>

<div class="content"></div>


<form id="myform" name="myform" action="./ok.php" method="post">
<td><textarea rows="6" cols="15" name="outputtext" style="width: 99%;"></textarea></td>
</form>



<button onclick="callApi2()">Start</button>


</html>

You should create a new variable outside of your function to store the ID in.

var maxTagId = null;

function callApi2() {

    $.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: false,
        url: "https://api.instagram.com/v1/tags/mango/media/recent?access_token=xxxxxxx" + ( maxTagId ? "&max_tag_id=" + maxTagId : ""),
        success: function(data) {

            maxTagId = data.pagination.next_max_tag_id;
        }
    });

}

Of course these variables are now in the global namespace, so ideally you'd put them in a self-invoking function. For more info see: http://marcofranssen.nl/writing-modular-javascript-without-polluting-the-global-namespace/

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