Calling Rest webservice using JQuery Ajax call , web service is returning JSON string

后端 未结 3 1255
离开以前
离开以前 2021-01-15 03:09

I have made a Rest Web Service:

package org.jboss.samples.rs.webservices;


import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.GET         


        
3条回答
  •  广开言路
    2021-01-15 03:28

    I think that you are not returning a valid json: try something like:

      return  "{\"name\":\"unknown\", \"age\":-1}"
    

    because this

    {
        "name": "unknown",
        "age": -1
    }
    

    is a valid JSON (You must use ", not ') while this is not

    {
        'name': 'unknown',
        'age': -1
    }
    

    You should also specify the datatype

     $.ajax({  
           type: "GET",  
           url: "http://localhost:8080/nagarro-0.0.1-SNAPSHOT/MyRESTApplication/dealInfo/2",  
           dataType: "json",  
           success: function(resp){  
             // we have the response  
             alert("Server said123:\n '" + resp.name + "'");  
           },  
           error: function(e){  
             alert('Error121212: ' + e);  
           }  
         });
    

提交回复
热议问题