我试图将jQuery ajax请求与Wicket结合起来,但我面临一个小问题......
请求第一次工作,但第二次失败。 js控制台说它是由于语法错误“意外令牌”而发生的。用firedebug查看请求,第一次看起来是正确的,但第二次没有响应。
第一次按下按钮同时调用“onRequest”和“neforeRender”,第二次调用“beforeRender”。
我不确定它的jQuery或Wicket是否是这里的恶棍...“onBeforeRender”被称为两个关系的事实似乎表明请求正在通过,但同时“onRequest”不是第二次被称为即使firedebug确认数据已发送......
这里的任何人都有关于问题的线索或理论吗?
下面是在提交按钮上的“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);
},
});
}
按下按钮时会调用“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();
}
}
“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);
}
}
}
答案 0 :(得分:0)
如果你有javascript异常,我发现新的Wicket 6实现不起作用(ajax只能第一次工作)。在我的情况下,它是AutoCompleteField,它添加了js并抛出:
TypeError: obj is null
[Break On This Error]
objonkeydown=obj.onkeydown;
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;
我只是将其移除,现在可以正常使用