简体   繁体   English

茉莉花模拟ajax调用的成功回调中没有位置标头

[英]No location header inside success callback for jasmine mocked ajax call

I am mocking an ajax request using jasmine-ajax as follows 我正在使用jasmine-ajax模拟ajax请求,如下所示

jasmine.Ajax.requests.mostRecent().respondWith({
    "status": 200,
    "responseText": '{}'
});

I am trying to get the 'Location' header for the request object passed to the ajax success callback as follows: 我正在尝试获取传递给ajax成功回调的请求对象的“ Location”标头,如下所示:

success(function(data, textStatus, request) {
    var url = request.getResponseHeader('Location');
    url = url.substring(...);
}

but it returns null and the jasmine test fails because substring function is called on null. 但它返回null,并且茉莉花色测试失败,因为在null上调用了子字符串函数。 What should I do? 我该怎么办? Note: I can access the 'Location' header just fine if the ajax request is not mocked. 注意:如果没有模拟ajax请求,我可以很好地访问“ Location”标头。

You could add that function to the object that you are returning: 您可以将该函数添加到要返回的对象中:

jasmine.Ajax.requests.mostRecent().respondWith({
  "status": 200,
  "responseText": '{}',
  "getResponseHeader": function(arg) { return 'customlocation';}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM