JQuery - $.ajax POST 不会将 .data 发送到 Web 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5596341/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:26:42  来源:igfitidea点击:

JQuery - $.ajax POST does not send .data to web server

jqueryajaxjsppost

提问by Eyeless Whim

I am using the JQuery $.ajax post command to invoke an ajax event on my web server:

我正在使用 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);
    }
});

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.

ajax 组件成功调用了网页,但不包含任何 post 数据。
即 - "(HttpServletRequest) request.getParameterMap.size() == 0"- 我期望 3,但我得到零。

Changing the above command from POST to a GET makes everything work just fine.

将上述命令从 POST 更改为 GET 会使一切正常。

TIA

TIA

回答by Eyeless Whim

The cause of the problem was found using FireBug and opening the opening the Net gadget.

问题的原因是使用 FireBug 并打开网络小工具找到的。

I'm seeing that the web server is responding with a status 302 on the call to the web page.

我看到 Web 服务器在调用网页时响应状态 302。

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.

扩展 Firebug/Net 中的 302 请求,并检查参数、标头、响应和 HTML,很快确定这是源自服务器的特定于应用程序的问题。

Thanks for everyone's feedback

感谢大家的反馈

回答by Timon. Z

Try this :

尝试这个 :

$.ajax({
    type: 'POST',
    url: '/foo.jsp',
    async: false,
    data: { fe1: "y", fe2: "m", fe3: "m" },
    complete: function(xmlRequestObject, successString){
        ymmReceiveAjaxResponse(xmlRequestObject, successString);
    }
});

It should work.

它应该工作。

回答by T.J. Crowder

Your code as quotedis fine (I've tried it locally).

引用的代码很好(我已经在本地尝试过)。

My guess is that the formParamsstring 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 thatcode instead.

我的猜测是formParams您问题中的字符串只是一个示例,实际上您正在做一些事情来动态生成该字符串,而问题在于代码。

For instance, are you sure you're escaping characters correctly (using encodeURIComponent)? Or better yet, let jQuery deal with it, like this:

例如,您确定正确转义字符(使用encodeURIComponent)吗?或者更好的是,让 jQuery 处理它,像这样:

$.ajax({
    type: 'POST',
    url: '/foo.jsp',
    async: false,
    data: {
        fe1: $("#somefield1").val(),
        fe2: $("#somefield2").val(),
        fe3: $("#somefield3").val()
    },
    complete: function(xmlRequestObject, successString){
        ymmReceiveAjaxResponse(xmlRequestObject, successString);
    }
});

If you pass in an object, jQuery will handle the URI-encoding for you. If you reallywant to do it yourself:

如果您传入一个对象,jQuery 将为您处理 URI 编码。如果你真的想自己做:

var formParams =
    "fe1=" + encodeURIComponent($("#somefield1").val()) +
    "fe2=" + encodeURIComponent($("#somefield2").val()) +
    "fe3=" + encodeURIComponent($("#somefield3").val());
$.ajax({
    type: 'POST',
    url: '/foo.jsp',
    async: false,
    data: formParams,
    complete: function(xmlRequestObject, successString){
        ymmReceiveAjaxResponse(xmlRequestObject, successString);
    }
});

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.

我没有对字段名称进行编码,因为这些名称中没有任何特殊字符;如果您的表单名称比这更有趣,您需要这样做。

回答by kmario23

After frustratingly trying for four hours, I found out that it is able to achieve this by setting the contentTypein ajax POSTas follows,

令人沮丧地尝试了四个小时后,我发现可以通过如下设置contentTypein ajax来实现这一点POST

var dataToSend = {
     "username" : $("#username").val(),
     "password" : $("#password").val()
};

$.ajax({
   type: "POST",
   url: "somepage.jsp",
   data: dataToSend,  
   contentType: "application/x-www-form-urlencoded; charset=UTF-8", //this is must
   success: function(datum, msg, textStatus){
        $("#result").html("<h3>" + "Status : " + msg + "</h3>")
        .fadeIn("slow");
  }
});

回答by Gary Green

Use success:

使用success

var formParams = "fe1=y&fe2=m&fe3=m";

$.ajax({
    type: 'POST',
    url: '/foo.jsp',
    async: false,
    data: formParams,
    success: function(data) {
       alert('response data = ' + data);
    }
});