将jQuery ajax与Wicket集成时的奇怪行为

时间:2022-11-24 13:40:12

Im trying to combine jQuery ajax requests with Wicket but I am facing a small problem...

我试图将jQuery ajax请求与Wicket结合起来,但我面临一个小问题......

The requests works the first time but the second time it fails. The js console says that it occurs because of an syntax error, "unexpected token". Looking at the request with firedebug it looks correct the first time but the second time there is no response.

请求第一次工作,但第二次失败。 js控制台说它是由于语法错误“意外令牌”而发生的。用firedebug查看请求它第一次看起来是正确的,但第二次没有响应。

The first time the button is pressed both "onRequest" and "neforeRender" is called, the second time though ONLY "beforeRender" is called.

第一次按下按钮时,第二次调用“onRequest”和“neforeRender”,第二次调用“beforeRender”。

Im unsure if its jQuery or Wicket thats the villain here... The fact that "onBeforeRender" is called both tiems seems to indicate that the request is coming through, bvut at the same time "onRequest" isnt called the second time even though firedebug confirms that the data is sent...

我不确定它的jQuery或Wicket是否是这里的反派...事实上“onBeforeRender”被称为两个联盟似乎表明请求即将通过,但同时“onRequest”并未被称为第二次即使firedebug确认数据已发送...

Anyone here got a clue or theory on what the problem is?

这里的任何人都有关于问题是什么的线索或理论?

Below is the jQuery code called on a "onClick" event on a submit button.

下面是在提交按钮上的“onClick”事件上调用的jQuery代码。

function getTestValue() 
{
var testArray = [ 1, 2, 3, 4, 5 ];
$.ajax({
    url : $('#mark').attr('json:callback.url'),
    type : 'post',
    cache : false,

    data : JSON.stringify(testArray),
    contentType : 'application/json',
    dataType : 'json',
    success: function(responseData) 
    {
        var values = responseData.sumPerTimeUnit;
        var labels = responseData.labels;
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) 
    {
        alert($('#mark').attr('json:callback.url'));
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(errorThrown);
    },
});
}

The panels "onBeforeRender" is called when the button is pressed, even if the request fails.

按下按钮时会调用面板“onBeforeRender”,即使请求失败也是如此。

public class AbstractJSONHandlerPanel extends Panel
{
private static final long    serialVersionUID = 1L;

private AbstractJSONBehavior JSONBehavior;

private Form mMarkUp;

public AbstractJSONHandlerPanel(String id)
{
    super(id);

    JSONBehavior = new AbstractJSONBehavior();

    mMarkUp = new Form("markupid")
    {
        @Override
        protected void onComponentTag(ComponentTag tag) {
            super.onComponentTag(tag);
            tag.put("json:callback.url", JSONBehavior.getCallbackUrl());
        }
    };
    mMarkUp.setMarkupId("mark");

    add(JSONBehavior);

    Button n = new Button("dummyButton");
    n.add(AttributeModifier.replace("onclick", "getTestValue();"));

    mMarkUp.add(n);
    add(mMarkUp);
}

@Override
protected void onBeforeRender()
{
    System.out.println("BEFORE RENDER");

    String callbackUrl = JSONBehavior.getCallbackUrl().toString();
    mMarkUp.add(AttributeModifier.replace("json:callback.url", callbackUrl));

    super.onBeforeRender();
}
}

The "onRequest" is only called the first time the button is pressed.

“onRequest”仅在第一次按下按钮时调用。

public class AbstractJSONBehavior extends AbstractAjaxBehavior
{
private static final long serialVersionUID = 1L;

public void onRequest()
{
    System.out.println("REQUEST");

    RequestCycle requestCycle = RequestCycle.get();
    readRequestData(requestCycle);
    writeResponseData(requestCycle);
}

private void readRequestData(final RequestCycle requestCycle)
{
    WebRequest wr = (WebRequest) requestCycle.getRequest();

    HttpServletRequest hsr = (HttpServletRequest) wr.getContainerRequest();

    try
    {
        BufferedReader br = hsr.getReader();

        String jsonString = br.readLine();
        if( (jsonString == null) || jsonString.isEmpty() )
        {
            System.out.println(" no json found");
        }
        else
        {
            System.out.println(" json  is :" + jsonString);
        }

        br.close();
    }
    catch( IOException ex )
    {
        throw new RuntimeException(ex);
    }
}

private void writeResponseData(RequestCycle requestCycle)
{
    WebResponse wr = (WebResponse) requestCycle.getResponse();

    HttpServletResponse hsr = (HttpServletResponse) wr
            .getContainerResponse();

    try
    {
        PrintWriter br = hsr.getWriter();

        List<Integer> data = new ArrayList<Integer>(6);
        data.add(350);
        data.add(421);
        data.add(976);
        data.add(1521);
        data.add(432);
        data.add(121);

        List<String> labels = new ArrayList<String>(6);
        labels.add("Jan");
        labels.add("Feb");
        labels.add("Mar");
        labels.add("Apr");
        labels.add("Maj");
        labels.add("Jun");

        StatisticsResponse resp = new StatisticsResponse(data, labels);

        Gson mGsonParser = new Gson();
        String json = mGsonParser.toJson(resp);

        System.out.println("RESPONSE: " + json);

        br.write(json);

        br.flush();
        br.close();
    }
    catch( IOException ex )
    {
        throw new RuntimeException(ex);
    }
}
}

1 个解决方案

#1


0  

I found that new Wicket 6 implementation does not work (ajax works only first time) if you have javascript exceptions. In my case it was AutoCompleteField which added js and it throws:

如果你有javascript异常,我发现新的Wicket 6实现不起作用(ajax只能第一次工作)。在我的情况下,它是AutoCompleteField添加了js,它抛出:

TypeError: obj is null
[Break On This Error]
objonkeydown=obj.onkeydown;

The full fragment of the wicket-autocomplete...js

wicket-autocomplete的完整片段...... js

function initialize(){
var isShowing = false;
// Remove the autocompletion menu if still present from
// a previous call. This is required to properly register
// the mouse event handler again (using the new stateful 'mouseactive'
// variable which just gets created)
var choiceDiv = document.getElementById(getMenuId());
if (choiceDiv !== null) {
isShowing = choiceDiv.showingAutocomplete;
choiceDiv.parentNode.parentNode.removeChild(choiceDiv.parentNode);
}
var obj = Wicket.$(elementId);
initialElement = obj;
objonkeydown=obj.onkeydown; 

I just remove it and it works now

我只是删除它,它现在有效

#1


0  

I found that new Wicket 6 implementation does not work (ajax works only first time) if you have javascript exceptions. In my case it was AutoCompleteField which added js and it throws:

如果你有javascript异常,我发现新的Wicket 6实现不起作用(ajax只能第一次工作)。在我的情况下,它是AutoCompleteField添加了js,它抛出:

TypeError: obj is null
[Break On This Error]
objonkeydown=obj.onkeydown;

The full fragment of the wicket-autocomplete...js

wicket-autocomplete的完整片段...... js

function initialize(){
var isShowing = false;
// Remove the autocompletion menu if still present from
// a previous call. This is required to properly register
// the mouse event handler again (using the new stateful 'mouseactive'
// variable which just gets created)
var choiceDiv = document.getElementById(getMenuId());
if (choiceDiv !== null) {
isShowing = choiceDiv.showingAutocomplete;
choiceDiv.parentNode.parentNode.removeChild(choiceDiv.parentNode);
}
var obj = Wicket.$(elementId);
initialElement = obj;
objonkeydown=obj.onkeydown; 

I just remove it and it works now

我只是删除它,它现在有效