blob: 94538e05f7d2e85dd40ad5f58faff30cadfb3869 [file]
<!--
#* Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.*#
-->
This example shows how an ActionResult can return a
<a href="http://www.json.org/" target="_blank" class="external">JSON</a> response.
<p/>
Click $link to call the server using Ajax.
<div id="result">
<!-- Ajax response will be set here -->
</div>
<script type="text/javascript" src="$context/assets/js/jquery-1.4.2.js"></script>
<script type="text/javascript">
// Register a function that is invoked as soon as the DOM is loaded
jQuery(document).ready(function() {
// Register a 'click' handler that makes an Ajax request
jQuery("#link-id").click(function(event){
// Make ajax request
makeRequest();
// Prevent the default browser behavior of navigating to the link
return false;
})
});
function makeRequest() {
var link = jQuery('#link-id');
var extraData = link.attr('id') + '=1';
var url = link.attr('href');
jQuery.get(url, extraData, function(data) {
// 'data' is the response received from the server
// Extract the 'msg' and 'date' values from the JSON response and output the result
var obj = jQuery.parseJSON(data);
jQuery("#result").html("<p class='infoMsg'>" + obj.msg + obj.date + "</p>");
}, {dataType:'json'});
}
</script>