【发布时间】:2013-05-22 22:00:07
【问题描述】:
我有一个简单的 ajax 发布到服务器..
$(".invite-team-members-submit-btn").click(function() {
$.post("invite_team_member", {
token: $("#token").val(),
email: $("#email").val(),
team: $("#team").val()
},"json")
.done(function (responseText) {
alert(responseText.response);
})
.fail(function (xhr,status,message) { alert("ERROR: " + message); })
.then(function () { alert("Something should happen."); });
});
返回的 JSON 如下所示...
{"response":"Person has been invited."}
我在控制台中的响应头是这样的......
Response Headers
Cache-Control max-age=0, private, must-revalidate
Connection close
Content-Type application/json; charset=utf-8
Date Wed, 22 May 2013 21:45:07 GMT
Etag "e5b5e12acbcc78372b2a861027b66c05"
Status 200 OK
Transfer-Encoding chunked
X-Request-Id d835ce021eff7733d67ebfcdd468bdf2
X-Runtime 0.007909
x-ua-compatible IE=Edge
我的控制台不仅告诉我我收到了 200 状态,而且我的响应包括我想要的 JSON。但是,在处理解析的 JSON 时,不会发出警报。请注意,我没有收到任何警报,甚至来自 then 函数。这可能是什么原因造成的?
【问题讨论】:
-
console.log(responseText)和console.log(typeof responseText)由于您正在返回 application/json 内容类型标头,jQuery 可能会为您解析它,然后在 jQuery.parseJSON 上出现错误,因为您正在尝试解析一个对象而不是一个字符串。 -
@KevinB 说了什么;如果您已经返回 JSON,请不要使用
parseJSON -
控制台日志中没有返回任何内容。它表现得好像服务器从未被击中,除非控制台在和我开玩笑。
标签: javascript jquery ruby-on-rails ajax json