http://localhost:2323
如果 jQuery 中的 $.Ajax 不起作用,如何从服务器获取 json 。json生成宽度java类:
public class Main {
public static void main(String[] arr) {
new Main().start();
}
protected void start() {
for (;;) {
try {
Socket remote = new ServerSocket(2323).accept();//port number
BufferedReader in = new BufferedReader(new InputStreamReader(
remote.getInputStream()));
PrintWriter out = new PrintWriter(remote.getOutputStream());
String str = ".";
while (!str.equals(""))
str = in.readLine();
out.println("HTTP/1.0 200 OK");
out.println("Content-Type: text/html");
out.println("Server: Bot");
out.println("");
out.print("{\"a\":\"A\",\"b\":\"asdf\",\"c\":\"J\"}");
out.flush();
remote.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
}
输出 {"a":"A","b":"asdf","c":"J"}。jquery脚本是
$(document).ready(function() {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://localhost:2323',//the problem is here
async: false,
data: {},
success: function(data) {
alert(data.a+' '+data.b+' '+data.c);
}
});
});
如果 url 是http://localhost
,那么它可以工作,如果我附加一个:portnumber,它就不起作用。如何从 url:portnumber 读取?
谢谢