2013-05-03 199 views
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?

感谢

+1

http://stackoverflow.com/questions/2099728/how-do-i-send -an-AJAX请求上-A-不同端口与 - jquery的 – Getz 2013-05-03 07:36:27

回答