开发者

First ajax call doesn't work

I'm trying ajax for the first time but it doesn't work.

This is "some.php" which handles the ajax call:

<?php
    echo "success";
?>

And this is the javascript that calls it:

<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>

Can you see where the problem is?

I should state I'm working un开发者_运维问答der wordpress and both files reside in \wp-content\themes\twentyten (maybe the url in the ajax call is wrong?)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜