This is my first attempt to prototype. I want to initiate an AJAX request that should get a JSON response and alert that. I have done following so far.
JS:
<script type="text/javascript">
function ajaxRequest() {
var url = '/ajaxresponse';
new Ajax.Request( url, {
method: 'get',
onSuccess: function(transport,json) {
alert(json ? Object.inspect(json) : "no JSON object");
},
onFailure: function(){
alert('Something went wrong...')
}
});
}
</script>
HTML:
<a href='javascript:ajaxRequest();'>Testing AJAX</a>
JSON source:
function ajaxresponse() {
// Data
$data = array("New York", "New Yorkshire", "New Jersey");
// encode and return json data
echo json_encode( $data );
}
On clicking “Testing AJAX” link I am getting following result in alert box:
no JSON object
Any idea ?
Thanks
I dont see any second variable passed to onSuccess handler in prototype. Look here. There is only transport object. So this should help: