我使用Dojoxhr将一些数据发布到ASP.NET MVC控制器:
xhr.post("/MyController", {
handleAs: "json",
data: {
contentIdentifier: 'abc123',
language: 'en'
}
}).then(function (response) {
console.log('Success.');
}, function (err) {
console.error(err);
});
控制器返回纯文本结果,我可以看到服务器的响应确实是200 OK。
但是,错误处理程序总是被触发的。
当输出错误到控制台时,我得到了类似的东西:
SyntaxError: Unexpected token S
message: "Unexpected token S"
response: {
options: TMP
status: 200
text: "Successfully pushed content to 1 instance(s)."
在我看来,错误是因为它无法将纯文本响应解析为JSON?也就是说,单词"Successfully"中的第一个"S"是什么打破了xhr?
响应的内容类型为text/plain
Doh。
handleAs参数根据Dojo文档:
用于处理响应负载的内容处理程序
换句话说,将handleAs设置为"text"解决了问题。