Wicket.Ajax.ajax工作不像ajax,但像普通的http查询一样

时间:2016-05-19 10:46:45

标签: ajax wicket wicket-7

我使用Wicket.Ajax.ajax与AbstractDefaultAjaxBehavior配对,将一些javascript计算数据发送到java。但事件从javascript触发并进入Java后,浏览器已被重定向到回调URL。

...web/product/1?7&6-1.IBehaviorListener.0-idsPanelPlace%3Floggged_id=332797

logggedidAjax = new AbstractDefaultAjaxBehavior() {

        @Override
        protected void respond(AjaxRequestTarget target) {
            StringValue loggged_vkid = getRequest().getQueryParameters().getParameterValue("loggged_id");
            String loggedId = (loggged_id != null) ? loggged_id.toString() : "null";
            logger.info("ajax has comming with logged ID " + loggedId);
        }

        @Override
        public void renderHead(final Component component, IHeaderResponse response) {
            super.renderHead(component, response);
            String componentMarkupId = getMarkupId();

            Map<String, Object> map = new HashMap<>();
            map.put("callbackUrl", logggedidAjax.getCallbackUrl());

            PackageTextTemplate ptt = new PackageTextTemplate(VKIDsPanel.class, "id_callback.js");
            OnDomReadyHeaderItem onDomReadyHeaderItem = OnDomReadyHeaderItem.forScript(ptt.asString(map));
            response.render(onDomReadyHeaderItem);
        }
    };
    add(logggedidAjax);

至于js代码 -

var wcall = Wicket.Ajax.ajax({ u: '${callbackUrl}' + '?loggged_id='+ response.session.mid });

为什么浏览器会重定向到网址,因为它是Ajax?如何防止重定向?

1 个答案:

答案 0 :(得分:0)

我不确定您的代码中出了什么问题,但以下内容应该更容易:

logggedidAjax = new AjaxEventBehavior("domready") {

  @Override
  protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
    super.updateAjaxAttributes(attributes);

    String loggedId = "return {'logged_id': response.session.mid}";

    attributes.getDynamicExtraParameters().add(loggedId);
  }

  @Override
  protected void onEvent(AjaxRequestTarget target) {
    StringValue loggged_vkid = getRequest().getQueryParameters().getParameterValue("loggged_id");
    String loggedId = (loggged_id != null) ? loggged_id.toString() : "null";
    logger.info("ajax has comming with logged ID " + loggedId);
  }
};
add(logggedidAjax);