[英]Dojo XHR treats 200 OK as error
I use Dojo xhr to post some data to an ASP.NET MVC controller: 我使用Dojo xhr将一些数据发布到ASP.NET MVC控制器:
xhr.post("/MyController", {
handleAs: "json",
data: {
contentIdentifier: 'abc123',
language: 'en'
}
}).then(function (response) {
console.log('Success.');
}, function (err) {
console.error(err);
});
The controller returns a plain text result, and I can see the response is indeed 200 OK from the server. 控制器返回纯文本结果,我可以看到服务器的响应确实是200 OK 。
However, the error handler is always triggered. 但是,错误处理程序总是被触发。
When outputting err to the console I get something like: 当输出err到控制台时,我得到类似:
SyntaxError: Unexpected token S
message: "Unexpected token S"
response: {
options: TMP
status: 200
text: "Successfully pushed content to 1 instance(s)."
It seems to me the error is because it cannot parse the plain text response as JSON? 在我看来,错误是因为它无法将纯文本响应解析为JSON? Ie the first "S" in the word "Successfully" is what's breaking xhr ?
即单词“ Successfully”中的第一个“ S”是xhr的中断内容吗?
The content type for the response is text/plain 响应的内容类型为text / plain
Doh. 卫生署。
The handleAs argument according to the Dojo docs: 根据Dojo文档的handleAs参数:
The content handler to process the response payload with. 用于处理响应有效载荷的内容处理程序。
In other words, setting handleAs to "text" solved the problem. 换句话说,将handleAs设置为“文本”即可解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.