为什么第一次 ajax 调用不起作用?

我第一次尝试使用Ajax,但是它不起作用。这是处理ajax调用的“ some.php”:

<?php
    echo "success";
?>

这是调用它的javascript:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript">

var msg;

$.ajax({
   type: "POST",
   url: "some.php",
   data: ({ })
   success: function(msg){
     alert( msg );
   }
 });
</script>

你能看到问题出在哪里吗?

我正在wordpress下工作,并且两个文件都位于\wp-content\themes\twentyten中(也许ajax调用中的网址是错误的?)

First of all remove the data:({}) which is pointless. you are also missing a , behind your data statement. this is most likely the issue.

if both the files is in the same directory, then the url should be correct.

However, i urge you to use a tool like FireBug in order to debug your problem further

You should run your script when the page has loaded (more precisely, when the DOM is ready). jQuery offers an event for that.

Your code could then look something like this:

$(document).ready(function(){
    $.ajax({
        type: "POST",
        url: "some.php",
        data: ({ })
        success: function(msg){
            alert( msg );
        }
    }
});

Two things to do:

  1. register a .fail callback. The code as it is will just call the alert() if it succeeds, otherwise, errors are not raised. See http://api.jquery.com/jQuery.ajax.

  2. check the web server log to see if some.php is exec'd and if so, what errors may be occurring on the server.