What I´m trying to accomplish: Click on image retrieves artist information from spotify api and shall show it as modal. I achieve to retrieve and show the requested information in console.log and window.alert. But I just don´t get it to handle it via modal. I`m fatfree (f3) php framework, bootstrap, jquery.
This is the jquery part:
(function(){
var artistId = "";
$(document).ready(function() {
let token =
"token";
// todo: get token from php variable
$("img").click(function() {
var artistId = $(this).attr("data-id");
$.ajax({
url: "https://api.spotify.com/v1/artists/" + artistId,
headers: { Authorization: "Bearer " + token },
success: function(result) {
$.ajax({
type: "POST",
data: {myData:result},
url: "/views/content/artistdetails.php",
success:
function(){
console.log('yes');
$('.modal').modal('show')
console.log(result);
},
error: function(e) {
console.log(e.message);
}
}
)
}
});
});
});
artistdetails.php for the modal (Note, I just copied the html from bootstrap, first I need to show it up)
<div class="modal" tabindex="-1" role="dialog" id="myModal" data-toggle="modal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="getCode">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
I went through a lot of articles concerning this topic, I just don´t find the error. Thanks for any help!
You need to have your modal in the same page where you do the ajax. So, If you want to show the modal, you just run $('.modal').modal('show')
, but if you want to show the response of Spotify API in the modal, you can do:
<div class="modal" tabindex="-1" role="dialog" id="myModal" data-toggle="modal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Spotify API Response</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="getCode">
<p id="getCodeText"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script>
(function() {
var artistId = "";
$(document).ready(function() {
let token =
"token";
// todo: get token from php variable
$("img").click(function() {
var artistId = $(this).attr("data-id");
$.ajax({
url: "https://api.spotify.com/v1/artists/" + artistId,
headers: {
Authorization: "Bearer " + token
},
success: function(result) {
console.log(result);
// Here you change the text of the <p></p> element in your modal's body.
$('#getCodeText').html(result);
// Here you show the modal.
$('.modal').modal('show');
}
});
});
});
});
</script>