Ajax 帖子在我的代码中不起作用我不知道原始代码(控制器代码)的确切问题请帮助我
class Ajax_post extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('url'));
}
function index()
{
$this->load->view('ajax_post');
}
function post_action()
{
if(($_POST['username'] == "") || ($_POST['password'] == ""))
{
$message = "Please fill up blank fields";
$bg_color = "#FFEBE8";
}elseif(($_POST['username'] != "myusername") || ($_POST['password'] != "mypassword")){
$message = "Username and password do not match.";
$bg_color = "#FFEBE8";
}else{
$message = "Username and password matched.";
$bg_color = "#FFA";
}
$output = '{ "message": "'.$message.'", "bg_color": "'.$bg_color.'" }';
}
}
?>
这是我的查看页面代码
<div id="form_message"></div>
<form name="ajax_form" id ="ajax_form" method="post">
Username/Email:*<input type="text" name="username" id="username" size="30" /><br/><br/>
Password:*<input type="password" name="password" id="password" size="30" /><br/><br/>
<input type="submit" value="Submit" name="login_submit" id="login_submit" onClick="getData();"/>
</form>
</div>
这是我的 javascript 代码 (ajax_post.js)
function getData(){
var username=$("#username").val();
var password=$("#password").val();
$.ajax({
type: "GET",
url: "http://localhost/CodeIgniter/index.php/ajax_post/post_action",
dataType: "json",
data: "username="+username+"&password="+password,
cache:false,
success:
function(data){
alert(data);
}
// $("#form_message").html(data.message).css({'background-color' : data.bg_color}).fadeIn('slow');
});
}
</body>
我整天坚持这件事..
请任何人告诉我做控制器和js代码的正确方法。