I have the following jQuery-enhanced Javascript within a HTML page:
<script>
$(document).ready(function(){
$("#sb").click(function(e){
e.preventDefault();
getusername = "Yes";
getpassword = "Yes";
getusername = encodeURIComponent(getusername);//url encodes data
getpassword = encodeURIComponent(getpassword);//url encodes data
$.ajax({
type: "POST",
url: "get_login.php",
data: {'username': getusername, 'password':getpassword},
dataType: "json",
success: function(data) {
$("#mes开发者_开发技巧sage_ajax").html("<div class='successMessage'>" + data.message +"</div>");
}
});
});
});
</script>
My php is as follows:
if(isset($_POST['username']) && isset($_POST['password'])){
echo json_encode(array('message' => 'Tester'));
}
The script isn't working however. I think it is to do with the way I am posting data from the browser. Perhaps I am incorrectly setting username and password and it is failing upon the isset statement?
Currently, the page does nothing. The button click event handler is triggering...but the message_ajax div in my HTML page isn't updating with correct response...
change the line
$("#message_ajax").html("<div class='successMessage'>" + data.message +"</div>");
to
$("#message_ajax").html("<div class='successMessage'>" + data.message +"</div>");
and try !!!
you need to change the mime header... your ajax is expecting json as a respons so you need to set the correct header.
header('Content-type: application/json');
http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests
精彩评论