jquery中的Ajax调用不起作用

I have a little problem with ajax and passing POST data from jquery to PHP, and then writing back returned value from php to div tag

This is the code:

JS

function jsAjax()
{

    $.ajax(
    {

        type:"POST",
        url:"http://localhost/anonsecrets/functions/jswrapper.php",
        data:{name:"Ilija", phone:"0343014"},
        success:function(data)
        {
            $("#msg").html(data);
        },
        error:function()
        {
            alert("Neuspenos");
        }
    });
};

PHP

<?php

echo $_POST['name'];

?>

And HTML

 <div id="msg"></div>
 <a href="" target="" onclick="jsAjax()"><div id="headerbutton">Best</div></a>

Modify your HTML to

<a href="#"

from

<a href="" 

So that your page will not be reloaded after clicking on the a tag which makes AJAX request

Do you have the entire function in some sort of onLoad statement? JQuery needs to be available to the DOM before it can be used.

http://api.jquery.com/ready/

Ankit is also correct with the a tag.

You can use jQuery click handler by adding id to link

Example:

HTML

<div id="msg"></div>
<a href="" id="msg-link" target=""><div id="headerbutton">Best</div></a>

JS

$('#msg-link').click(function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "test.php",
        data: {name: "Ilija", phone: "0343014"},
        success: function(data)
        {
            $("#msg").html(data);
        },
        error: function()
        {
            alert("Neuspenos");
        }
    });
});

You can also do the following to your HTML

<a href="" target="" onclick="jsAjax(); return false;"><div id="headerbutton">Best</div></a>

Which ensures that the click is ignored by the browser, asides that, you should ensure that the URL of the page is in the localhost domain also, to avoid problems due to CORS (http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) issues

Jonathan,

You didn't describe what happens when you click the link. If the page reloads without making the ajax call, Ankit's solution will work. And if you're having hard time to find the exact reason, try to test it on Firefox after installing Firebug extension. In Firebug console, you'll see Javascript error if any.