0

Someone answered in my past question and provided me with the code necessary to pull a datetime from a server. All is well it's just that I don't know how to execute it! for example, I want to the action to happen on pageload, how can I do that?

here's the Ajax/JSONP code I was given:

$.ajax({
    url: 'http://timeapi.org/utc/now.json',
    dataType: 'jsonp'
})
.done(function(response) {
    // response = {"dateString":"2012-03-06T02:18:25+00:00"}
    console.log(response.dateString);// "2012-03-06T02:18:25+00:00"
});

P.S. does the "$" refer to JQuery? I am not very familiar with this syntax...the $.ajax and the .done?

7
  • 2
    Yes. You need to learn jQuery. Commented Mar 6, 2012 at 16:16
  • 1
    I'd use $.getJSON - api.jquery.com/jQuery.getJSON - but i'm pretty sure the type should be just 'json' p.s. yes, $ refer to the jQuery "object" Commented Mar 6, 2012 at 16:17
  • double check API docs that they deliver jsonp and that you don't need to provide any special callback name for jsonp wrapper. Many people assume that an API that delivers json also delivers jsonp, not true Commented Mar 6, 2012 at 16:19
  • I mean I am mostly concerned about actually running this in my code. Do I like include this inside a function that I call onload? Commented Mar 6, 2012 at 16:21
  • You have to include the jQuery library in your page and then wrap this script in a document ready wrapper, $(document).ready(function() { // your ajax code here }); Start here: docs.jquery.com/Tutorials:How_jQuery_Works Commented Mar 6, 2012 at 16:22

1 Answer 1

3

You definitely have to check out jQuery.

Basically, $ refers to the jQuery object, and .ajax() and .done() are functions which are properties of this object. They can be concatenated by a common technique of returning the jquery-context at the end of each function.

Simplified you have something like this:

jQuery.function(parameters).anotherfunction(parameters)

You should start with this basic tutorial.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.