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

发布于 2024-10-31 14:30:47 字数 532 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泪之魂 2024-11-07 14:30:47

使用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

暖阳 2024-11-07 14:30:47

试试这个:

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

它应该有效。

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.

待天淡蓝洁白时 2024-11-07 14:30:47

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

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

例如,您确定正确转义字符吗(使用 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);
    }
});

如果您传入一个对象,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);
    }
});

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

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:

$.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 really want to do it yourself:

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.

瀞厅☆埖开 2024-11-07 14:30:47

经过四个小时的沮丧尝试后,我发现可以通过在ajax POST 中设置 contentType 来实现这一点,如下所示,

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");
  }
});

After frustratingly trying for four hours, I found out that it is able to achieve this by setting the contentType in ajax POST as follows,

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");
  }
});
柏林苍穹下 2024-11-07 14:30:47

使用成功

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);
    }
});

Use 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);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文