0

I have problem in callback function in jquery.

lets start from the beginning. My restful web service url is this

http://localhost :35055/new/webresources/generic/1

and it will return

{'name':'get', 'age':58}

and my jquery to interact with this web service is

$(document).ready(function() {
    $.ajax({
        dataType: "json",
        url: '/webresources/generic/1',
        type: 'GET',
        data: ' ',
        success: function() {
            alert('PUT completed');
        }
    });
});

which is linked in the jsp page

http://localhost:35055/new/tool.jsp 

there is no alert message. Please suggest me a way and also debug this code please.

this is the restful webservice

    @GET
    @Produces("application/json")
    public String getJson( @PathParam("venki") int empno ) {
        if(empno==1)
        {
      return "{'name':'get', 'age':'58' }";
        }
        return "{'name':'error', 'age':'58' }";
        }

}
itsme
  • 135
  • 3
  • 5
  • 15

2 Answers2

1

You can expose the error through the error property of the $.ajax call if you aren't familiar with using Fiddler or the built in browser tools to inspect network traffic.

$(document).ready(function() {
    $.ajax({
        dataType: "json",
        url: '/new/webresources/generic/1',
        type: 'GET',
        data: ' ',
        success: function() {
            alert('PUT completed');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
          }
    });
});
Scott
  • 13,735
  • 20
  • 94
  • 152
  • thank you so much...it returns some error...its says....200, SyntaxError: JSON.parse: expected property name or '}' – itsme Mar 16 '14 at 16:08
  • You most likely need to look at the JSON you're sending back from the service to make sure it's well formed. – Scott Mar 16 '14 at 16:18
  • thank you so much @Scott for the help. this is the coding is very useful for me to find the exact error. – itsme Mar 16 '14 at 16:42
0

Retry by correcting url setting value to '/new/webresources/generic/1' like this:

$.ajax({
  url: '/new/webresources/generic/1', 
  success: function() { alert('PUT completed'); }
});

According to your updated Java code of web service implementation, you should refer to answer of How correctly produce JSON by RESTful web service to find the correct java code.

Community
  • 1
  • 1
shawnzhu
  • 7,233
  • 4
  • 35
  • 51