我正在使用 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/