André Carvalho André Carvalho - 14 days ago 7
HTML Question

Why my Ajax not working

I'm try to use the Marvel API to do a school work and I did a function to do request to the Marvel server but I only receive the following object:
Object
What is the problem?



function GetComic() {
var publicKey = 'e99561b7db2b414b085c39de1c7ed4ab';
var privateKey = 'dc00398114b3a9a4fbd20d8a318d54530c11659f';
var timestamp = Date.now();;
$.ajax({
url: 'https://gateway.marvel.com/v1/public/comics',
type: 'GET',
dataType: 'json',
data: {apikey: publicKey, hash: md5(timestamp+privateKey+publicKey),ts: timestamp}
})
.done(function() {
console.log("success");
})
.fail(function(r) {
console.log(r);
})
.always(function() {
console.log("complete");
});

}

$('#btSearch').click(function(){
GetComic();
});




Answer

Your ajax function seem to correct but I did't see where you define the response for it so try this. Then we'll see what you get.

.done(function(response) {
    console.log("success",response);
})

Update

You said that your ajax not work when you add button. It's because when you use the button it's will submit your form then the page is reload. So to make it's not submit the form. You can just easy edit your click event like this.

$('#btSearch').click(function(e) {
    e.preventDefault();  //add this line to prevent form to submit
    GetComic();
});