Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  • Thread Tools
  • Rate This Thread
  1. #1
    New to the CF scene
    Join Date
    May 2016
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Using JSONP to request information from IP Request

    Hi there,

    I am very new to JS so please bare with me. I am wanting to use promises in order to retrieve information from a JSONP request. Ive requested the country code however it appears to not be working. Ive done an if statement to ask if myFunction2 does not equal GB to show a random picture. However this field from this JSON request DOES equal GB.

    Code:
    <script>    
    			var promise = new Promise(function(resolve, reject) {
    			  // do a thing, possibly async, then…
    			 function myFunction2(){
                        $.ajax({
                        url: '//freegeoip.net/json/',
                        type: 'POST',
                        dataType: 'jsonp',
                        success: function (location) {
                            myipaddress = location.country_code;
                            return location.country_code;
                            }
                     })
    			  };
    			  if (myFunction2 !== "GB") {
    			    resolve("Stuff worked");
    			  } else {
    			    reject(new Error("It broke"));
    			  }
    			});
    			
    			promise
    				.then(result => console.log(result))
    			  .catch(error => {
    			  	fallbackimg = new Image();
    			  	fallbackimg.src = "https://i.stack.imgur.com/N6iem.png?s=64&g=1";
    			  	fallbackimg.alt ="";
    			  	document.getElementById("demo").appendChild(fallbackimg);
    			  });
    		</script>
    Could someone please advise where to look to correct this issue please

    Thankyou
    Rustic

  2. #2
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,123
    Thanks
    30
    Thanked 250 Times in 248 Posts
    If myFunction2 is a function
    that returns "GB" then its like this ...
    if (myFunction2() !== "GB") {
    Cleverness is serviceable for
    everything,
    sufficient in nothing.

  3. Users who have thanked DaveyErwin for this post:

    rusticblonde (05-10-2016)

  4. #3
    New to the CF scene
    Join Date
    May 2016
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    If myFunction2 is a function
    that returns "GB" then its like this ...
    if (myFunction2() !== "GB") {
    Hi Davey,

    Thankyou very much for pointing this out to me

    Rustic


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •