when I look at the header sent by the test.php in Chrome inspector it says action : startjson
but I cant retrieve this variable $_GET['action']
, the array is empty.
test.php :
<body>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script language="JavaScript">
$(document).ready(function(){
$("#testjson").click(function(e){
startJsonSession();
return false;
});
function startJsonSession(){
$.ajax({
url: "test2.php?action=startjson",
cache: false,
dataType: "json",
complete: function(data) {
username = data.username;
alert(username);
}
});
}
});
</script>
<button id="testjson">
toto
</button>
</body>
test2.php :
<?php
if ($_GET['action'] == "startjson") {
startjsonSession();
}
function startjsonSession() {
$items = '';
print json_encode(array(
"username" => "bob",
"items" => array(
"item1" => "sandwich",
"item2" => "applejuice"
)
));
}
There is difference between sucess and complete , The complete function does not receive the 'data' parameter, so if your action depends on the data then it won't work there.
Complete : complete callback option fires, when the request finishes, whether in failure or success. It receives the jqXHR object, as well as a string containing the success or error code.
Success : callback option is invoked, if the request succeeds. It receives the returned data, a string containing the success code, and the jqXHR object.