-3

I want to get the content from http://ipinfo.io/127.0.0.1/country and store it into a cookie named, country with the expiry of 24 hours.

I've used PHP and did this several times but have no idea about JQuery. I've used following snippets in PHP

$county=file_get_contents("http://ipinfo.io/127.0.0.1/country");
setcookie("country", $country, time() + (86400 * 30), "/");
Disk01
  • 347
  • 3
  • 11
  • Look for "*Get answers to practical, detailed questions*" at http://stackoverflow.com/tour – kosmos Jul 04 '15 at 21:01
  • [Ajax](http://www.w3schools.com/ajax/default.asp) and [cookies](http://www.w3schools.com/js/js_cookies.asp) – kosmos Jul 04 '15 at 21:15
  • kmsdev you should see http://stackoverflow.com/questions/3340797/can-an-ajax-response-set-a-cookie if not sure about cookies in ajax – Disk01 Jul 04 '15 at 21:21
  • Excuse me for the short comment. I don't know what you don't understand. You have to use ajax to retrieve the data and Cookies to store it. I saw the link, but are you trying to get the contents of a webpage and store it into a cookie or you want to store a cookie within the ajax request? What did you try so far? If you want your php code converted to Javascript, my second comment already answered you. – kosmos Jul 04 '15 at 21:44
  • The very 1st paragraph of the questions describes my query. In simple words I want to fetch the content of the above mentioned URL and store it in a cookie. It will be very helpful if you can convert the php into js or ajax – Disk01 Jul 04 '15 at 21:47
  • Actually, I gave you two links where you can learn how to do it by yourself. I can help your with a code but I can't work for free, sorry buddy. Check out this link: http://stackoverflow.com/help/on-topic. You can make easy the ajax call if you use the [jQuery framework for Javascript](http://jquery.com/). – kosmos Jul 04 '15 at 21:58

1 Answers1

1

Gets the contents of the page, and saves it to a cookie using jQuery and Javascript

$.get( "http://ipinfo.io/127.0.0.1/country", function( data ) {
   var d = new Date();
    d.setTime(d.getTime() + (24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = "theCountry=" + data+ "; " + expires;
});
Carl K
  • 974
  • 7
  • 18