0

在服务器上,当发布一个帖子时,它会使用创建的对象重定向到一个新的 url。如何从该请求中获取该网址?

$(".trend-item .trend-form").on("ajax:success", function(e, data, status, xhr) {
  return my_function();
});

我需要查询包含和 ID 的已创建 url,以便我可以将其连接到响应中。

4

1 回答 1

1

尝试获取X-Xhr-Redirected-To:from xhr.getAllResponseHeaders()

/**
 * XmlHttpRequest's getAllResponseHeaders() method returns a string of response
 * headers according to the format described here:
 * http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method
 * This method parses that string into a user-friendly key/value pair object.
 */
function parseResponseHeaders(headerStr) {
  var headers = {};
  if (!headerStr) {
    return headers;
  }
  var headerPairs = headerStr.split('\u000d\u000a');
  for (var i = 0; i < headerPairs.length; i++) {
    var headerPair = headerPairs[i];
    // Can't use split() here because it does the wrong thing
    // if the header value has the string ": " in it.
    var index = headerPair.indexOf('\u003a\u0020');
    if (index > 0) {
      var key = headerPair.substring(0, index);
      var val = headerPair.substring(index + 2);
      headers[key] = val;
    }
  }
  return headers;
}

资源

于 2013-10-29T08:53:40.310 回答