【问题标题】:Need to get the response and to have change in ajax jquery GUI需要获得响应并在 ajax jquery GUI 中进行更改
【发布时间】:2014-10-05 13:56:04
【问题描述】:

我有来自 Jquery ajax 的 restful webservices 调用并在 json 对象中获取响应。 jQuery HTML:

$.ajax({
                                        url: "mcrs/object/api/Lighting",
                                        type: "POST",
                                        traditional: true,
                                        dataType: "json",                                        
                                        contentType: "application/x-www-form-urlencoded",
                                        async: "false",
                                        data:JSON.stringify(postdata),                                    
                                        success: function (ResData) {
                                        },
                                        error: function (error, data) {
                                            console.log(error);
                                        }
                                    });                                
                                }       
                            });  

并在java中为restful webservices编写响应对象

@POST
    @Path("api/{subsystem}")
    @Produces("application/json")
    public Response changeStatus(@PathParam("subsystem") String subsystem,
            /*@FormParam("result")*/ String result) {
    }

我得到了下面的回复,这是正确的

changeStatus:88 - 响应 OutboundJaxrsResponse{status=200, reason=OK, hasEntity=true, closed=false, buffered=false}

根据响应需要更新标签标签中的值是开/关的状态。 str = '

状态:' + '' + onoffStat + '';

如何在 ajax 中实现这一点,并希望每 2 秒刷新一次 div 标签。

请帮帮我。

【问题讨论】:

  • 您要更新的“状态”是什么?

标签: java javascript jquery ajax json


【解决方案1】:

您必须使用 Javascript 轮询。试试这样的:

function ajaxPoll(){
     $.ajax({
                                        url: "mcrs/object/api/Lighting",
                                        type: "POST",
                                        traditional: true,
                                        dataType: "json",                                        
                                        contentType: "application/x-www-form-urlencoded",
                                        async: "false",
                                        data:JSON.stringify(postdata),                                    
                                        success: function (ResData) {
                                            tag.str = 'Status : ' + '' + ResData.status + '';
                                        },
                                        error: function (error, data) {
                                            console.log(error);
                                        }
                                    });                                
                                }       
                            });  
}

setInterval(ajaxPoll, 2000);

希望你能解决:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多