jQuery - ajax - post - json请求 - 在某些URL中没有post body

时间:2013-03-02 02:11:55

标签: javascript jquery ajax post

我已经坚持了一段时间了。我在这里有ajax请求:

$.ajax({
    url: UPDATE_USER_INFO_URL ,
    type: "POST",
    dataType: "json",
    contentType: "application/json",
    data: JSON.stringify({user:'user'}),
    success: function (data, textStatus) {
        if(data["statusCode"] && data["statusCode"] == 1) {
            _callback(1,data);
        }
        else {
            _callback(0,data);
        }
    },
    error: function (jqXHR, textStatus){
        _callback(0, {});
    }           
});

如果我将UPDATE_USER_INFO_URL设置为特定网址,则提琴手在正文中不显示任何内容。如果我将UPDATE_USER_INFO_URL设置为其他内容(甚至是无效的网址),则会将{user:'user'}放入fiddler中的正文中。

使用原始UPDATE_USER_INFO_URL:

POST http://10.35.50.26:8080/SelfServiceWs/user/session/upduserinfo HTTP/1.1
Accept: application/json, text/javascript, ; q=0.01
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Referer: http://10.35.50.26:8080/SelfService/
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: 10.35.50.26:8080
Connection: Keep-Alive
Pragma: no-cache
Cookie: JSESSIONID=0BF9D9CCCE9030E60AB0BCE5F6562CD8
Authorization: Negotiate TlRMTVNTUAABAAAAl4II4gAAAAAAAAAAAAAAAAAAAAAGAbAdAAAADw==
Content-Length: 0

Chage url to / SelfServiceWs / abcdef

POST http://10.35.50.26:8080/SelfServiceWs/abcdef HTTP/1.1
Accept: application/json, text/javascript; q=0.01
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Referer: http://10.35.50.26:8080/SelfService/
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: 10.35.50.26:8080
Content-Length: 15
Connection: Keep-Alive
Pragma: no-cache
Cookie: JSESSIONID=9E79779805579A7964E03AAD76DF043B

{"user":"user"}

我还有很多其他的ajax调用,都按预期工作。

我错过了一件小事。

2 个答案:

答案 0 :(得分:1)

我想出来了。

我有一个对url / user / ssoauth的身份验证servlet过滤器,意外地(对我来说),它在/ user路径(包括/ user / session / upduserinfo)下调用URL来发送Authorization标头。将过滤器移动到/ user / auth / ssoauth停止客户端以在调用user / session / upduserinfo时发送授权标头并修复问题。

<filter-mapping>
  <filter-name>SecurityFilter</filter-name>
  <url-pattern>/user/ssoauth</url-pattern>
</filter-mapping>

导致每个客户端在/ user之后调用URL以发送授权标头。

我今天学到了新东西!

答案 1 :(得分:0)

试试这个

data: JSON.stringify({'user':'user'}),