Reputation: 31
I am unable to get a response from the ipify API call with the following Javascript and JQuery code. If it matters, I am using Gulp to run the project. Below is sample code that I'd imagine would work with a callback:
function callAPI( callback ) {
$.ajax("https://api.ipify.org/?format=json",
{
success: function(response) {
callback(response);
}
});
}
function alertIP( response ) {
alert(response);
}
callAPI(alertIP);
On page load, a alert box should appear with the user's IP address. However, what actually appears is a blank alert box. Upon further inspection of the response object, I can see that it is null.
The API point works fine when I enter it through browser or through Postman, so I have no idea why it doesn't work in my code. I tried some other async solutions (explicitly writing async:true, using .when().then()) but it appears that the API always fails when I try to run it (in a .done() .fail() call, the code executes the .fail() always).
Any ideas on what I might be doing incorrectly?
Upvotes: 0
Views: 562
Reputation: 31
I was able to solve my problem. The problem wasn't the code, but rather the browser that was running it. Turns out that the Brave Browser was blocking the API request. After running it on Google Chrome the code snippet was able to work.
Upvotes: 2