2

I have a RESTful web service which runs on Glassfish Application Server. When I invoke the web services with /GET HTTP method on cURL, stored entries are fetched to console. I want to make a jQuery REST client - when I click the button, it must alert me to the returned JSON or XML entries. But in success method, nothing happened. My html page looks like below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Başlıksız Belge</title>

</head>

<body>
<input type="submit" name="kaydet" id="kaydet" value="Kaydet" />
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
var restURL="http://localhost:43842/KodcuComRESTful/kodcuRS/yazilar";

$('#kaydet').click(function(){
      $.ajax({
    type: 'GET',
    url: restURL,
    dataType:"json",
    success: renderList,
      });
      return false;
});


function renderList(data) {
    alert(data);
}
</script>
</body>
</html>

When I observed the request and response in Live HTTP headers program, it seems everything is OK. What is the problem? enter image description here

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
UsTa
  • 329
  • 2
  • 5
  • 10

4 Answers4

8

I have implemented something you want to. Here is the code. And it works completely fine.

Hope it helps you.

 <html>
 <script type="text/javascript" src="jQuery.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
        $("#submit").click(function(){
        $.ajax({
            type: 'GET',
            url: "http://localhost/page1.html",
            success:function(data){
             alert(data);
            }
        });
    return false;
    });
});
</script>
</head>

<body>
<input type="button" id="submit" value="submit" />
</body>
 </html>
vijay
  • 2,034
  • 3
  • 19
  • 38
  • Thanks vj shah, your code works correctly! but, whats the failure in my code? I put the html page in same domain with REST endpoint. – UsTa Jun 17 '12 at 09:48
  • Ajax by default works asynchronously. Try adding `async:false` to the dictionary . – rassa45 Aug 15 '16 at 16:53
1

Two things to check:

  1. Comment out dataType:"json", and see if it is working. Maybe this is not JSON.
  2. Is this HTML on the same domain as restURL points to? If not, you may need JSONP.
Community
  • 1
  • 1
Pol
  • 5,064
  • 4
  • 32
  • 51
  • No, it's in a filesystem, how can i implement jsonp – UsTa Jun 17 '12 at 09:17
  • 1
    You need to call JavaScript function from JSON result. So you need to modify a little bit code on server. See link on "JSONP" word :) – Pol Jun 17 '12 at 09:19
1

There is a comma - "," after success function line, remove it as it is the last line?

Currently it is:

success: renderList,

change it to: success: renderList

Final Html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Başlıksız Belge</title>

</head>

<body>
<input type="submit" name="kaydet" id="kaydet" value="Kaydet" />
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
var restURL="http://localhost:43842/KodcuComRESTful/kodcuRS/yazilar";

$('#kaydet').click(function(){
      $.ajax({
    type: 'GET',
    url: restURL,
    dataType:"json",
    success: renderList
      });
      return false;
});


function renderList(data) {
    alert(data);
}
</script>
</body>
</html>
Max
  • 36
  • 3
0

You can also use this code

<script>
$("#submit").click(function(){

var val = $("#text").val();

$.get('get.php?id='+val,function(data,status){

if(status == "success")
{
  $("#show").val(data);
}
});

});
</script>
Gar
  • 852
  • 13
  • 20