4
如何從服務器獲取json http://localhost:2323
如果jQuery中的$ .Ajax不起作用。將JSON產生寬度java類:jQuery,ajax,端口號
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」, 「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?
感謝
http://stackoverflow.com/questions/2099728/how-do-i-send -an-AJAX請求上-A-不同端口與 - jquery的 – Getz 2013-05-03 07:36:27