将jqXHR定义为“失败”的原因

时间:2018-03-28 01:08:41

标签: ajax spring-security

我对spring security后端有一个ajax请求。由于某种原因会发生什么,.fail永远不会发生。我发现每个请求都会发生 // Creates request object function makeRequest(method, module, endpoint) { return req = { method, url: serverBaseUrl + module + '/' + endpoint }; } // Function to return POST promise function post (module, endpoint, data) { let req = makeRequest('POST', module, endpoint); req.data = data; return $.ajax(req); } ,即使请求通过,后端的登录也会起作用,它会返回状态码为200的响应。那么什么将jqXHR定义为失败,我该怎么做需要添加响应,以便它可以正常工作吗?

这是我的ajax请求:

HTTP/1.1 200 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 0
Date: Wed, 28 Mar 2018 01:02:20 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=8D6265E912D5DFCF418238F18586AFE1; Path=/; HttpOnly
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

以下是我从弹簧安全设置中得到的回复:

/**
 * Deep diff between two object, using underscore
 * @param  {Object} object Object compared
 * @param  {Object} base   Object to compare with
 * @return {Object}        Return a new object who represent the diff
 */
const difference = (object, base) => {
  const changes = (object, base) => (
    _.pick(
      _.mapObject(object, (value, key) => (
        (!_.isEqual(value, base[key])) ?
          ((_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value) :
          null
      )),
      (value) => (value !== null)
    )
  );
  return changes(object, base);
}

1 个答案:

答案 0 :(得分:0)

毕竟我的问题出在我的$.ajax请求中。我将dataType参数添加为application/json。这似乎无效。当我在失败时从请求中打印错误时找到它。从这里得到答案: jquery ajax call return JSON parsing error