Wordpress: POST to .php with Ajax


Wordpress: POST to .php with Ajax

我在让这段代码工作时遇到了很多麻烦:

<script type = "text/javascript">
 jQuery(document).ready(function($){ 
 $('#submitbutton').click(function(){
     var email=document.getElementById('emailadd').value;
     var password=document.getElementById('pass').value;
     $.ajax({
     type: "POST",
     url: "/wp-content/themes/roots-master/login.php",
     data: {emailaddress:'email', password:'password'},
     dataType: "text",
     success:function(response){
     alert("success");
     }
     });
 });
}); 
</script>

当我注释掉 $.ajax 函数时,javascript 工作正常(我用警报对其进行了测试)。但是,$.ajax 部分似乎不起作用。有人有什么建议吗?

当我将代码更改为以下内容时,javascript 似乎也无法正常工作:

   $('#submitbutton').click(function(){
     var email=document.getElementById('emailadd').value;
     var password=document.getElementById('pass').value;
     $.ajax({
     alert(password);
     });
     });

我正在使用wordpress,所以我没有在头上放任何jquery的链接。相反,我将以下内容放入我的函数中.php然后jquery开始工作。

wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery               /1/jquery.min.js', false, '1.9.1', false);
wp_enqueue_script('jquery');

更新:出于某种原因,当我在 $.ajax 之后发出警报时,它可以工作!

jQuery(document).ready(function($){ 
$('#submitbutton').click(function(){
     var email=document.getElementById('emailadd').value;
     var password=document.getElementById('pass').value;
     $.ajax({
     dataType: "html",
     type: "POST",
     evalScripts: true,
     url: "/wp-content/themes/roots-master/login.php",
     data: {emailaddress:'email', password:'password'},
     dataType: "text",
     success:function(){
     alert("success");
     }
     });
     alert("testing");
 });
}); 

有谁知道为什么会这样?

解决了!!它应该看起来像这样:

 $('#submitbutton').click(function(j){
     j.preventDefault();

我缺少防止默认()...咚!