当我在本地主机上运行我们的 Analytics 应用程序时,(来自调试器)它完美加载。但是,当我上传到服务器时,GET 请求被取消。
这是网络响应的屏幕截图:
你会建议我如何解决这个问题?
这是来自服务器的响应代码
/**
* Sends a JSON response for an object (200)
* @param objectToEncode
* @throws Exception
*/
private void sendJSONResponse(Object objectToEncode, Request baseRequest, HttpServletResponse response) throws Exception{
//Encode object into JSON
String jsonString = new Gson().toJson(objectToEncode);
byte[] utf8JsonString = jsonString.getBytes("UTF8");
response.setContentLength(utf8JsonString.length);
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/x-json;charset=UTF-8");
response.setStatus(200);
try {
//Actually send the response
response.getWriter().write(jsonString);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Javascript 请求:
/*
Draws the chart for average session length by day
*/
function drawUsersGender() {
//Apit to get the data from
var api = GET_USERS_SEX;
//Request data (using jquery/ajax)
$.getJSON(api,function(data){
//Start a days and seconds array
var result = [['gender', 'number']];
//Iterate over the genders
for (var gender in data){
//Get the value pair and push
var entry = [gender, parseInt(data[gender])];
result.push(entry);
}
//Parse to google data
var data = google.visualization.arrayToDataTable(result);
//Display options
var options = {
title:'Gender for registered users',
'width':600,
'height':400
};
//Draw the chart
var chart = new google.visualization.PieChart(document.getElementById('genderChart'));
chart.draw(data, options);
});
}