javascript - Ajax returning blank response -
i'm working on simple script want see request , response sent server , recieved @ client via ajax. server returning status 500
. doing wrong? below script.
javascript:
<script> function loginjs() { var xmlhttp; if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code 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(); } ?>
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 trace error;
2) check console log javascript errors.