2013年9月6日星期五

getjson Inter

Request address ( directly in the browser request )

http://server.llkk.com:8181/Server.do?gid=100000&utype=1&callback=?

return the string

?({server:[{stype:'server',serverinfo:[{sid:10010001,sname:'封测1服'}]})



$.getJSON("http://server.llkk.com:8181/Rank.do?sid=1&gid=1&type=level", function (data) {
        alert("s");//不会执行...
    });



public class Server extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("P3P", "CP=CAO PSA OUR");
response.setContentType("text/javascript");
response.setCharacterEncoding("utf-8");

HashMap serverMap = null;
List<Object[]> serverList = (List<Object[]>) null;
String callback = request.getParameter("callback");
String gid = request.getParameter("gid");
int utype = Integer.parseInt(request.getParameter("utype"));
StringBuffer str = new StringBuffer();
str.append("{server:[");

serverMap = Constant.serverList;
if (serverMap.size() != 0) {
serverList = (List<Object[]>) serverMap.get(gid);
if (serverList != null && serverList.size() > 0) {
Iterator i = serverList.iterator();
int index = 0;
while (i.hasNext()) {
index++;
Object[] sInfo = (Object[]) i.next();
if (utype == 0) {
if (sInfo[4].equals("A")) {
if (sInfo[3] == null) {
str.append("{stype:'server',serverinfo:[{sid:" + sInfo[1] + ",sname:'" + sInfo[2] + "'}");
} else {
if (sInfo[3].equals(1)) {
str.append("{stype:'newserver',serverinfo:[{sid:" + sInfo[1] + ",sname:'" + sInfo[2] + "'}");
} else if (sInfo[3].equals(0)) {
str.append("{stype:'server',serverinfo:[{sid:" + sInfo[1] + ",sname:'" + sInfo[2]+ "'}");
}
}
}
} else if (utype == 1) {
if (sInfo[3] == null) {
str.append("{stype:'server',serverinfo:[{sid:" + sInfo[1] + ",sname:'" + sInfo[2] + "'}");
} else {
if (sInfo[3].equals(1)) {
str.append("{stype:'newserver',serverinfo:[{sid:" + sInfo[1] + ",sname:'" + sInfo[2] + "'}");
} else if (sInfo[3].equals(0)) {
str.append("{stype:'server',serverinfo:[{sid:" + sInfo[1] + ",sname:'" + sInfo[2]+ "'}");
}
}
}
if (index < serverList.size()) {
str.append(",");
}
}
}
}
str.append("]}");

PrintWriter out = response.getWriter();
out.print(callback+"("+str.toString()+")");
out.flush();
out.close();
}
}

------ Solution ------------------------------------- -------
return json format issues now ... wood has seen escaping the place !
------ For reference only -------- -------------------------------
My question is : Why does not perform getJSON Lane , java code wrong do
------ For reference only ------------------------------------- -
third wrong, should be:

$.getJSON("http://server.llkk.com:8181/Server.do?gid=100000&utype=1&callback=?", function (data) {
alert("s")//不执行
});


------ For reference only ----------------------------------- ----
because you are now writing or ajax, instead of JSONP protocol
------ For reference only ------------------- --------------------
cross-domain it. Url address in the background request access to data , and then through the front ajax call back method .
------ For reference only -------------------------------------- -
is JSON format problems , do not escape ,

?({server:[{stype:'server',serverinfo:[{sid:10010001,sname:'封测1服'}]})
//格式错了,右边少了"]}"


------ For reference only ---------------------------------- -----
use . ajax it seems the problem when using this method

没有评论:

发表评论