在Ajax响应尾随空格 [英] Trailing spaces in Ajax response
问题描述
我想使用 Qunit 测试一些code,但我有一些问题Ajax调用。我甚至不能让他们用最简单的Ajax调用使用jQuery方法正确测试。这些问题似乎是一个尾随空格被追加到textResponse,无论我做什么。
I am trying to use Qunit to test some code, but I have some problems with Ajax calls. I cannot even get them to test correctly with the simplest Ajax call using jQuery methods. The problems seems to be that a trailing space is appended to the textResponse, no matter what I do.
我的初始code是像
asyncTest('Ajax calls', function() {
expect(1);
$.get('ajax.txt', {}, function(response) {
equal(response, 'foo', 'Ajax calls work correctly');
});
setTimeout(function() {
start();
}, 600);
});
其中, ajax.txt
是包含olny的字符的文本文件富
。这个测试失败,报告
where ajax.txt
is a text file containing olny the characters foo
. This test fails, reporting
正确的Ajax调用工作,预计:富的结果:富,差异:<打击>福罢>福
Ajax calls work correctly, expected: "foo" result: "foo ", diff:
"foo""foo "
我已经然后尝试以下操作:
I have then tried the following:
- 在我对富(包括空格) 测试
- 在我做了
response.replace('','')
测试之前 - 在我有各种不同的ajax.txt文件的字体编码
- 我已经从Firefox和Chrome,每次清除缓存测试它
- 我手动为警报内部测试其相等,甚至==比较
- I have tested against "foo " (including a trailing space)
- I have done
response.replace(' ', '')
before testing - I have varied the font encoding of the ajax.txt file
- I have tested it both in Firefox and Chrome, each time cleaning the cache
- I have manually tested for equality inside an alert, even with == comparison
但在任何情况下,我能得到匹配。例如在第一个变种我得到了令人费解的答案
but in no case I was able to get a match. For instance in the first variant I got the puzzling answer
正确的Ajax调用工作,预计:富的结果:富,差异:富
Ajax calls work correctly, expected: "foo " result: "foo ", diff: "foo "
我现在就是稍微疯了。我能一直在可能做错了什么?
I am now going slightly mad. What could I have been possibly doing wrong?
推荐答案
可以 $修剪()
(jQuery的修剪,因为IE浏览器&LT; 9没有它本身)的结果,这样的:
You can $.trim()
(jQuery trim, since IE<9 doesn't have it natively) the result, like this:
equal($.trim(response), 'foo', 'Ajax calls work correctly');
为什么的是这种情况发生?这可能是一个格式错误,例如Unix的VS被你匍匐在那里的Windows行结尾。
Why is this happening? It's likely a formatting error, e.g. Unix vs Windows line endings that are creeping in there on you.
这篇关于在Ajax响应尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!