Ajax响应中的尾随空格(Trailing spaces in Ajax response)

编程入门 行业动态 更新时间:2024-10-26 15:25:47
Ajax响应中的尾随空格(Trailing spaces in Ajax response)

我正在尝试使用Qunit来测试一些代码,但我在Ajax调用方面遇到了一些问题。 我甚至无法使用jQuery方法使用最简单的Ajax调用来正确测试它们。 问题似乎是,无论我做什么,都会在textResponse上附加一个尾随空格。

我的初始代码是这样的

asyncTest('Ajax calls', function() { expect(1); $.get('ajax.txt', {}, function(response) { equal(response, 'foo', 'Ajax calls work correctly'); }); setTimeout(function() { start(); }, 600); });

其中ajax.txt是一个包含ajax.txt字符foo的文本文件。 报告失败了

Ajax调用正常工作,预期:“foo”结果:“foo”,diff: “foo” “foo”

然后我尝试了以下内容:

我测试了“foo”(包括尾随空格) 我在测试之前完成了response.replace(' ', '') 我改变了ajax.txt文件的字体编码 我每次都在清理缓存时在Firefox和Chrome中测试过它 我已经手动测试了警报内的相等性,即使是使用==比较

但在任何情况下我都无法得到一场比赛。 例如,在第一个版本中,我得到了令人费解的答案

Ajax调用正常工作,预期:“foo”结果:“foo”,diff:“foo”

我现在有点生气了。 我可能做错了什么?

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.

My initial code was something like

asyncTest('Ajax calls', function() { expect(1); $.get('ajax.txt', {}, function(response) { equal(response, 'foo', 'Ajax calls work correctly'); }); setTimeout(function() { start(); }, 600); });

where ajax.txt is a text file containing olny the characters foo. This test fails, reporting

Ajax calls work correctly, expected: "foo" result: "foo ", diff: "foo" "foo "

I have then tried the following:

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 calls work correctly, expected: "foo " result: "foo ", diff: "foo "

I am now going slightly mad. What could I have been possibly doing wrong?

最满意答案

您可以$.trim() (jQuery修剪,因为IE <9本身没有它)结果,如下所示:

equal($.trim(response), 'foo', 'Ajax calls work correctly');

为什么会这样? 这可能是一个格式化错误,例如Unix与Windows行结尾在你身上蔓延。

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');

Why is this happening? It's likely a formatting error, e.g. Unix vs Windows line endings that are creeping in there on you.

更多推荐

本文发布于:2023-07-31 09:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   Ajax   Trailing   response   spaces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!