UsTa
UsTa

Reputation: 349

Jquery Ajax /GET method nothing happend when success

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

Upvotes: 2

Views: 48305

Answers (4)

Rahul Bhaskar
Rahul Bhaskar

Reputation: 1

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>

Upvotes: 0

vijay
vijay

Reputation: 2034

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>

Upvotes: 8

Pol
Pol

Reputation: 5134

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.

Upvotes: 1

Max
Max

Reputation: 36

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>

Upvotes: 1

Related Questions