0

I'm new to jQuery and Ajax. I'm following a tutorial, and having a problem with this.

I have an anchor tag with id getcomments and the following JavaScript:

<script>
$(function() {
   $('#getcomments').click(function() {
       $.ajax({
          url       : "req.html",
          success   : function(response) {
               console.log(response);
          }
       });
       return false;
   });
});

And an HTML file called req.html in the same folder with a mock comment.

When I inspect the log I only get the "document" in firebug, and no actual get request.

I've tried doing something actual in the success function also, appending the response.

Nothing happens, what am I doing wrong here?

6
  • What does req.html look like? Commented Nov 21, 2011 at 22:14
  • you could use console.log() in the first line of the click function to check if the event is triggered and perhaps provide us with some more info. Commented Nov 21, 2011 at 22:14
  • How do i provide the html? it dosent allow me to post the html here Commented Nov 21, 2011 at 22:20
  • @Paragonbliss Of course it does. Make it be code. <script> tags are HTML, btw. Commented Nov 21, 2011 at 22:22
  • Use a jsFiddle to post your thml and javascript and link to it Commented Nov 21, 2011 at 22:23

1 Answer 1

1

If it's a local file it won't make a request.

That you see Document in Firebug means it's working. Set the contents of a div to the response, after adding a dataType: "html" to the Ajax call--it will be the contents of the HTML file.

  $(function() {
    $('#getcomments').click(function() {
        $.ajax({
           url       : "req.html",
           dataType  : "html",
           success   : function(response) {
             $("#foo").append(response);
                console.log(response);
           }
        });
        return false;
    });
  });
Sign up to request clarification or add additional context in comments.

4 Comments

How do i do that, im not entirely sure how you mean
@Paragonbliss How do you do what? Cut and paste?
Sorry the code wasent there when i commented, but thank you very much that worked, before the datatype i tried what you said, but i was getting an error saying the node couldent be inserted in the hierachy at that point, anyways sorry i am so newbish to this, but this helped me, thank you :)
@Paragonbliss It was assuming an XML document; clicking on the Document line in Firebug will show you what the browser (thinks it) knows about the requested document. Glad you worked it out :)

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.