JQuery, ajax, port number

How to get json from server http://localhost:2323 if $ .Ajax in jQuery does not work. Json generated width java class:

 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); } } } } 

which outputs {"a": "A", "b": "asdf", "c": "J"}. And the jquery script is there

 $(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); } }); }); 

if url is http://localhost , then it works, if I add: port number, it does not work. How to read from URL: port number?

thanks

+4
source share
1 answer

Specifying a port in ajax calls will not work due to the same origin policy ( http://en.wikipedia.org/wiki/Same_origin_policy ). This means that the URL must have the same domain and port as the server where the script is located.

Also note that this question has already been asked and is one of the first results when searching on google - Can I specify a port in an ajax call

+4
source

Source: https://habr.com/ru/post/1478923/


All Articles