json jquery post request

<script type="text/javascript">
    $(document).ready(function() {
        $("a").click(function() {

            var content = $('#content').html();

            var data = {"content":content};


            $.ajax({
                type: "POST",
                dataType: "json",
                url: "ajax.php",
                data: {content:content},
                success function (data) {
                    alert('Hello!');
                }

            });


        });

    });        
</script>

<div id="content"><?php echo $content; ?></div>

ajax.php

echo json_encode($_POST['content']); ?>

Nothing happens... WhatI really want to achieve is to get that alert box and get the return data, but I am lost since I don't get any errors or nothing.

Answers


You miss " : " after success

<script type="text/javascript">
    $(document).ready(function() {
        $("a").click(function() {

            var content = $('#content').html();

            var data = {"content":content};


            $.ajax({
                type: "POST",
                dataType: "json",
                url: "ajax.php",
                data: {content:content},
                success: function (data) {
                    alert('Hello!');
                }

            });


        });

    });        
</script>

<div id="content"><?php echo $content; ?></div>

As @sofl said, if you change it to success:function (data) { it will work!

Just remember that the $("a") from $("a").click(function() { called when click in a link tag like <a href"">.

If you are using an input ou button with a class="a" you should change the code to $(".a").click(function() {

(just add a . before a)

PS: If you're using a link, you should set the href="" to href="#" to work.


Need Your Help

Is there any way to stop/interrupt a CSS3 transition animation in Firefox?

firefox css3 transitions

I am using FF version 13.0 and have a transition animation set to occur on :hover. I would like the element to return to it's original state as soon as I am no longer hovering. But what happens is...

Installing modules in Python without command line

python pygame pip

Hi I am going interstate and will only have my laptop with me, I do not have admin rights on it and the use of Cmd is banned. I want to be able to use Pygame on my laptop. How can I install the mod...