开发者

Jquery - webservices

开发者 https://www.devze.com 2023-04-10 17:59 出处:网络
JQuery is meant for writing client side scripts. I read, we c开发者_开发问答an call webservices in JQuery. Can you please give me a scenario need of calling webservices on the client side. thanks !Sce

JQuery is meant for writing client side scripts. I read, we c开发者_开发问答an call webservices in JQuery. Can you please give me a scenario need of calling webservices on the client side. thanks !


Scenario-Assuming,you want to retrieve the country list in the drop down by making the ajax call to a web service through JQuery.

Below is the Example Of JQuery call from Spring thymleaf template

<script th:inline="javascript">
    $("document").ready(function() {
        $("select#country").change(function() {
            var selectedCountry = $("#country option:selected").val();
            $.ajax({
                type : "GET",
                url : "/TestApplication/selectedCountry/" + selectedCountry,
                cache : false,
                timeout : 600000,
                success : function(data) {
                    $('#feedback').html(data);

                }

            });
        });

    });
</script>

In the above example a service is called(/TestApplication/selectedCountry/) to retrieve the set of countries.


Simple ajax call via GET :

On client:

PHP: (ex: view.php)

<span>value:</span><span id="ajaxValue"></span>

jQuery: (ex: view.php)

$.get('ws/getValueById.php?id=<?php echo $currentId ?>', function(data) {
  $('#ajaxValue').html(data);
});

On server:

PHP (ex: ws/getValueById.php)

function getValueById($id) {
  // database fetch
}
if (!isset($_GET['id'])) die('missing parameter id');
die (getValueById($_GET['id']));
0

精彩评论

暂无评论...
验证码 换一张
取 消