weixin_33720452 2015-04-23 06:41 采纳率: 0%
浏览 219

Ajax返回空白响应

I'm working on a simple script where I just want to see request and response that is sent to server and then recieved at client via Ajax. The server is always returning Status 500. What am I doing wrong? Below is my script.

Javascript:

<script>
function loginJs()
{

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET","http://my-website.com/ajax_exp.php",true);

xmlhttp.onreadystatechange=function()
{  

 alert("State "+xmlhttp.readyState+" Status "+xmlhttp.status);
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
    alert(xmlhttp.responseText);
 }
}
xmlhttp.send();
}
</script>

ajax_exp.php

<?php

header('Access-Control-Allow-Origin: http://my-website.com/ajax_exp.php');
add_action('wp_ajax_my_action',        'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');

function my_action()
{
$username = 'username';
$password = 'password';

echo $username;
die();
}


?>
  • 写回答

3条回答 默认 最新

  • weixin_33736048 2015-04-23 06:44
    关注

    1) try removing

     header('Access-Control-Allow-Origin: http://my-website.com/ajax_exp.php');
     add_action('wp_ajax_my_action',        'my_action');
     add_action('wp_ajax_nopriv_my_action', 'my_action');
    

    replacing with

     my_action();
    

    You can easily trace the error;

    2) Check console log for any javascript errors.

    评论

报告相同问题?