ajax - Play 服务器发送的 JSON 响应格式错误

标签 ajax json jquery playframework

我正在使用 jquery Ajax 向客户端发送数据并从客户端获取回复。 我使用 Play Framework 作为后端。

AJAX:

$.ajax({
    type: "GET",
    url: '/authenticate',
    data: {'type':type, 'redirectURL':getRedirectURL},
    contentType: "application/json; charset=UTF-8",
    dataType: 'json'
}).success(function( msg, txtStatus, jqXHR) {
    console.log("asdasd5= "+msg);
    console.log("asdasd5= "+msg.authUrl);
    console.log("asdasd5= "+jqXHR.authUrl);
    window.location = msg;
});

服务器数据创建:

response.setContentTypeIfNotSet("text/plain; charset=UTF-8");
Logger.info("response content type ="+response.contentType);
renderJSON("{\"authUrl\": " + authUrl +"}");

服务器正在发送类似“www.mywebsite.com/yoohoo/auth/1231”的内容

但是Ajax收到的响应是���������������{"authUrl": www.mywebsite.com/yoohoo/auth/1231}

由于这些奇怪的字符,jQuery 无法解析收到的 JSON 响应。 请帮忙解决这个问题。

谢谢

最佳答案

我注意到您正在发回(服务器端)“text/plain”的 MIME 类型。也许切换到“application/json”。此外,您可以在客户端解析 JSON 之前获取返回字符串并从字符串中去除所有垃圾。如果您再次在服务器端更改某些内容并意外引入新的/不同的“垃圾”,这将有助于清理。

关于ajax - Play 服务器发送的 JSON 响应格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18190757/

相关文章:

javascript - 了解如何为我的用户制作 Flair(javascript 片段)

sql - 如何在 SQL 存储过程中组合值

javascript - 时间输入向上箭头上的keyup

javascript - 嵌套 json 传入 ajax 调用

java - Ajax 不工作 JSF 2.2

php - 在 CakePHP 中更新没有表单数据的数据库

当我只想要父对象时,JSONPath 查询从嵌套对象返回同名键

javascript - Sublime Text 2 : replace text from JSON using regular expression

javascript - 如何使用 Jquery 或 Javascript 显示 AJAX 调用 div 的数据?

javascript - 多个div指向同一个标签