I've been trying to get ajax to work the whole day and this my last hope, i wrote a simple test to see if i get any response, the xhr.status is 0 instead of 200, and the xhr.responseText is undefinded. I call a simple (php file) which generates a json object, my code is as follows(i tried using $.ajax but it didnt help either):
$(function(){
$('#checkin').click(function(){
var ajaxRequest;
var connection = ajaxFunction();
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.open("GET", "places.php", true);
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
alert(console.error(ajaxRequest.status));
}
}
ajaxRequest.send(null);
}
});
})
and my php file is as follows:
<?php
header('Content-type: application/json');
require "SQLquery.php";
$places = new SQLquery;
$places->db_query("SELECT * FROM places");
echo json_encode($places->results);
?>
any help will be much appreciated. Thanx
Here is a simple implementation using jQuery's $.ajax() and json.
Javascript:
function sendAjax() {
// The data you want to pass to places.php
var params = {
'checkin' : true
};
// Construct the call
ajax = $.ajax({
url: '/places.php',
type: 'GET',
data: params,
dataType: 'json'
});
// Send it and say which function you'll
// use to handle the response
ajax.done(sendAjaxDone);
}
// Handle the response
function sendAjaxDone(response) {
console.log(response);
}
places.php
header('Content-type: application/json');
require "SQLquery.php";
if ($_GET['checkin']) {
$places = new SQLquery;
$places->db_query("SELECT * FROM places");
echo json_encode($places->results);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.