1

我正在尝试创建一个 POST 请求,不幸的是,似乎从未发送过 POST 的正文。

下面是我正在使用的代码。当用户单击链接时调用代码,而不是表单“提交”按钮。它运行没有错误,调用正在调用的 servlet,但是,正如我之前提到的,POST 的主体似乎从未被发送。

我可以验证请求正文永远不会发送,因为我可以访问被调用的 servlet。

我试过用“参数”代替“requestBody”。我也尝试过使用参数字符串 (x=a?y=b)。我还验证了“ckULK”确实包含有效值。

有任何想法吗?

new Ajax.Request(sURL,
{
    method: 'POST'
    , contentType: "text/x-json"
    , requestBody: {ulk:ckULK}
    , onFailure:
        function(transport)
        {
            vJSONResp = transport.responseText;
            var JSON = eval( "(" + vJSONResp + ")" );
            updateStatus(JSON.code + ": " + JSON.message);
        } // End onFailure
    , onSuccess: 
        function(transport) 
        {
            if (200 == transport.status)
            {
                vJSONResp = transport.responseText;
            }
            else
            {
                log.value += "\n" + transport.status;
            }
         } // End onSuccess
}); // End Ajax.request
4

3 回答 3

6

These are the kind of situations where Firebug and Firefox are really helpful. I suggest you install Firebug if you don't have it and check the request that is being sent.

You also definitely need to stick to parameters instead of requestBody.

This:

new Ajax.Request(sURL,
{
    method: 'POST',
    parameters: 'hello=world&test=yes',
    onFailure: function(transport) {
        vJSONResp = transport.responseText;
        var JSON = eval( "(" + vJSONResp + ")" );
        updateStatus(JSON.code + ": " + JSON.message);
    },
    onSuccess: function(transport) {
        if (200 == transport.status) {
            vJSONResp = transport.responseText;
        } else {
            log.value += "\n" + transport.status;
        }
     }
});

Should definitely work.

于 2008-10-19T05:53:42.100 回答
1

Simply pass the data as parameters to the Ajax Request constructor:

new Ajax.Request(url, {
  method: 'POST', 
  parameters: {
    hello: "world", test: "test"
  },
  onSuccess: function(transport){
    var data = transport.responseText.evalJSON();
  }
});
于 2008-10-20T07:42:31.697 回答
0

When I hit this problem the solution was to remove contentType from Ajax.Request options.

In answer by Paolo Bergantino contentType is not defined in Ajax.Request options.

From unknown reasons params defined in parameters wasn't sent (FF didn't show them and my server saw empty POST body).

One of my coworkers suggested to use postBody instead of parameters but I didn't try it.

于 2012-07-12T08:21:36.033 回答