java 如何使用 Ext.Ajax.request 将参数传递给 servlet?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16952717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters to servlet using Ext.Ajax.request?
提问by JS11
I have an extjs form from which I am trying to post parameter using Ext.Ajax.request to the servlet. Call is working and servlet is being called but for some reason parameter's value isn't being send. I'll post my code, can anyone tell me what I am doing wrong. Thanks in advance.
我有一个 extjs 表单,我试图从中使用 Ext.Ajax.request 将参数发布到 servlet。呼叫正在工作并且正在调用 servlet,但由于某种原因参数的值没有被发送。我会发布我的代码,谁能告诉我我做错了什么。提前致谢。
This is the call from ExtJS form:
这是来自 ExtJS 表单的调用:
buttons: [{
text: 'Search',
handler: function(){
var fName = Ext.getCmp("fName").getValue();
Ext.Ajax.request({
url : 'LookUPCustomer',
method: 'POST',
headers: { 'Content-Type': 'application/json'},
params : fName, // this value isn't being passed to servlet
success: function ( result, request ) {
var resultData1 = JSON.parse(result.responseText);
},
failure: function ( result, request ) {
resultData = JSON.parse(xmlhttp.responseText);
}
});
}];
and here is servlet code:
这是servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
// value of fName is null, not being passed from the form
String fName = request.getParameter("fName");
// does some processing....
// print back to the form
response.setContentType("application/json");
out.println(jsArray);
}
回答by jeff
The params parameter should be a JSON object with key, value pairs. Here's an example:
params 参数应该是一个带有键值对的 JSON 对象。下面是一个例子:
params: {
firstName: 'Jeff',
lastName: 'Tester'
}
or to plug in your variable
或插入您的变量
params: { fName: fName }
回答by Hariharan
As you said, u are using extjs 4.0.7. it uses extraparams. So you need to code like below
正如你所说,你使用的是 extjs 4.0.7。它使用额外参数。所以你需要像下面这样编码
Before sending just validate whether fName contain required value.
在发送之前只需验证 fName 是否包含所需的值。
Ext.Ajax.request({
url : <URL>,
method: 'POST',
extraParams :{ fName : fName },
success: function ( result, request ) {
var resultData1 = JSON.parse(result.responseText);
},
failure: function ( result, request ) {
resultData = JSON.parse(xmlhttp.responseText);
}
});
Thanks
谢谢