Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从rest服务发送json对象,以便在客户端javascript上进行解析_Java_Jquery_Ajax_Json_Jsp - Fatal编程技术网

如何从rest服务发送json对象,以便在客户端javascript上进行解析

如何从rest服务发送json对象,以便在客户端javascript上进行解析,java,jquery,ajax,json,jsp,Java,Jquery,Ajax,Json,Jsp,我只想将一个JSON对象(使用ajax)从我的服务器返回到客户端,这样我就可以读取客户端的数据了 @GET @Produces("application/json") @Consumes("application/json") @Path("/getStatus/") public void getStatus( @Context HttpServletRequest request, @Context HttpServletResponse re

我只想将一个JSON对象(使用ajax)从我的服务器返回到客户端,这样我就可以读取客户端的数据了

  @GET
  @Produces("application/json")
  @Consumes("application/json") 
  @Path("/getStatus/")

  public void getStatus(
      @Context HttpServletRequest request,
      @Context HttpServletResponse response) throws ServletException,
      IOException
  {

      //create the JSON Object to pass to the client
      JSONObject object=new JSONObject();

      response.setContentType("text/javascript");    

      try
      {  
            object.put("name", nameDataFromClass);
            object.put("status",someData);

       }
       catch(Exception e)
       {  
            throw new ServletException("JSON Hosed up");  
       }  

       String json = object.toString();  
       response.getOutputStream().println(json);   
  }
这将是JSP的客户端,我想在页面上提取数据

<html>
<head>

<!-- Calls in jQuery file -->
<script src="jquery.js"></script>

<title>JQuery Test</title>

<script>

    $.getJSON("http://localhost:8080/scout/rest/admin/mamba/getStatus",
    function(json) 
    {  
        alert("Server naame: " + json.name);  
    });  



</script>

</head>
<body>



</body>
</html>

JQuery测试
$.getJSON(“http://localhost:8080/scout/rest/admin/mamba/getStatus",
函数(json)
{  
警报(“服务器名称:+json.name”);
});  

Jackson库应该负责将json对象编组到您的对象,反之亦然。只需创建一个简单的POJO,如下所示:

public class Mystatus{
   public String name;
   public String status;
   public Mystatus(){}  // a default empty constructor is needed
   public Mystatus(String name,String status){
     this.name=name;
     this.status=status;
   }
}
然后从RESTful Web服务返回此对象:

@GET
@Produces("application/json")
@Consumes("application/json") 
@Path("/getStatus/")

public Mystatus getStatus(
  @Context HttpServletRequest request,
  @Context HttpServletResponse response)
{
  response.setContentType("text/javascript");
  return new Mystatus("Hello","World");
}
@GET
@生成(“应用程序/json”)
@使用(“应用程序/json”)
@路径(“/状态”)
//服务器:8080/server/rest/status
公共字符串getStatus(
@上下文HttpServletRequest请求,
@上下文HttpServletResponse)引发异常
{
//创建一个字符串来保存JSON
字符串json;
集合svr=SomeHashMap.getStuff().values();
JSONArray jArray=新的JSONArray();
用于(服务器i:svr)
{
JSONObject m=新的JSONObject();
ServerStatus status=i.getStatus();
m、 put(“id”,i.getId());
m、 put(“name”,i.getName());
m、 put(“status”,status.getState());
加上(m);
}
json=jArray.toString();
}
setContentType(“text/javascript”);
response.getOutputStream().print(json);
response.flushBuffer();
返回null;
}
index.jsp

<head>
<script src="jquery-1.6.js"></script>
     <!--AJAX FOR STATUS PAGE REFRESH -->
     <script type="text/javascript">

    //when page is ready do the following
    $(document).ready(function()
    {
        // Disable caching of AJAX responses
        $.ajaxSetup ({cache: false});

        //set interval of refresh
        setInterval(doAjaxStuff, 1000);

        //function to call to fire off ajax
        function doAjaxStuff()
        {
            $.ajax
            ({
                url: "status", // <-- this refers to the Controller function above called "status()"
                dataType: 'json',
                success: function(json) 
                {
                    //traverse throught each element in the incoming JSON object
                    for(var i = 0; i< json.length; i++)
                    {
                        if(json[i].status ==  "ACTIVE")
                        {
                            $("#Status"+json[i].id).html("Running");
                        }


                    }               
                }
            });
        }
    });
    </script>

</head>

//当页面准备就绪时,请执行以下操作
$(文档).ready(函数()
{
//禁用AJAX响应的缓存
$.ajaxSetup({cache:false});
//设置刷新间隔
设置间隔(DOA,1000);
//函数调用以触发ajax
函数doAjaxStuff()
{
$.ajax
({

url:“状态”,//那么什么对您不起作用呢?客户端没有收到任何数据。我的restful java是否应该返回?是否缺少一些jquerry/ajax片段?请求是否确实到达java层?没有-只是检查了。是否需要为ajax部分实现更多内容?好的,请求正在返回。尝试写入并刷新响应。不确定为什么在其中包含“回调”位。顺便说一句,这可能会使json无效。
<head>
<script src="jquery-1.6.js"></script>
     <!--AJAX FOR STATUS PAGE REFRESH -->
     <script type="text/javascript">

    //when page is ready do the following
    $(document).ready(function()
    {
        // Disable caching of AJAX responses
        $.ajaxSetup ({cache: false});

        //set interval of refresh
        setInterval(doAjaxStuff, 1000);

        //function to call to fire off ajax
        function doAjaxStuff()
        {
            $.ajax
            ({
                url: "status", // <-- this refers to the Controller function above called "status()"
                dataType: 'json',
                success: function(json) 
                {
                    //traverse throught each element in the incoming JSON object
                    for(var i = 0; i< json.length; i++)
                    {
                        if(json[i].status ==  "ACTIVE")
                        {
                            $("#Status"+json[i].id).html("Running");
                        }


                    }               
                }
            });
        }
    });
    </script>

</head>