JQuery - $.ajax POST 不会将 .data 发送到 Web 服务器
我正在使用 JQuery $.ajax post 命令在我的 Web 服务器上调用 ajax 事件:
var formParams = "fe1=y&fe2=m&fe3=m";
$.ajax({
type: 'POST',
url: '/foo.jsp',
async: false,
data: formParams,
complete: function(xmlRequestObject, successString){
ymmReceiveAjaxResponse(xmlRequestObject, successString);
}
});
ajax 组件成功调用网页,但它不包含任何 post 数据。
即 - “(HttpServletRequest) request.getParameterMap.size() == 0”
- 我期望 3,但得到零。
将上述命令从 POST 更改为 GET 使一切正常。
TIA
I am using the JQuery $.ajax post command to invoke an ajax event on my web server:
var formParams = "fe1=y&fe2=m&fe3=m";
$.ajax({
type: 'POST',
url: '/foo.jsp',
async: false,
data: formParams,
complete: function(xmlRequestObject, successString){
ymmReceiveAjaxResponse(xmlRequestObject, successString);
}
});
The ajax component successfully calls the web page, but it does not include any of the post data.
ie - "(HttpServletRequest) request.getParameterMap.size() == 0"
- I'd expect 3, but am getting zero.
Changing the above command from POST to a GET makes everything work just fine.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用FireBug并打开打开的Net小工具找到了问题的原因。
我看到 Web 服务器在调用网页时以状态 302 进行响应。
扩展 Firebug/Net 中的 302 请求,并检查参数、标头、响应和 HTML,很快发现这是源自服务器的应用程序特定问题。
感谢大家的反馈
The cause of the problem was found using FireBug and opening the opening the Net gadget.
I'm seeing that the web server is responding with a status 302 on the call to the web page.
Expanding upon the 302 request in Firebug/Net, and examining the Params, Headers, Response, and HTML quickly identified that it was an application specific issue originating on the server.
Thanks for everyone's feedback
试试这个:
它应该有效。
Try this :
It should work.
您引用的代码很好(我已经在本地尝试过)。
我的猜测是,您问题中的
formParams
字符串只是一个示例,实际上您正在做一些事情来动态生成该字符串,问题在于那个 代码代替。例如,您确定正确转义字符吗(使用
encodeURIComponent< /代码>
)?或者更好的是,让 jQuery 处理它,如下所示:
如果您传入一个对象,jQuery 将为您处理 URI 编码。如果您真的想自己做:
我没有对字段名称进行编码,因为这些名称中没有任何特殊字符;如果您的表单名称比这更有趣,您就需要这样做。
Your code as quoted is fine (I've tried it locally).
My guess is that the
formParams
string in your question is just an example, and in reality you're doing something to generate that string on the fly, and the problem lies in that code instead.For instance, are you sure you're escaping characters correctly (using
encodeURIComponent
)? Or better yet, let jQuery deal with it, like this:If you pass in an object, jQuery will handle the URI-encoding for you. If you really want to do it yourself:
There I haven't encoded the field names because those names don't have any special chars in them; you need to if your form names are more interesting than that.
经过四个小时的沮丧尝试后,我发现可以通过在ajax
POST
中设置contentType
来实现这一点,如下所示,After frustratingly trying for four hours, I found out that it is able to achieve this by setting the
contentType
in ajaxPOST
as follows,使用
成功
:Use
success
: