Java JQuery、ajax、端口号
如何从服务器获取jsonJava JQuery、ajax、端口号,java,jquery,ajax,Java,Jquery,Ajax,如何从服务器获取jsonhttp://localhost:2323如果jQuery中的$.Ajax不起作用。json由java类生成: public class Main { public static void main(String[] arr) { new Main().start(); } protected void start() { for (;;) { try { So
http://localhost:2323
如果jQuery中的$.Ajax不起作用。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读取:端口号
谢谢由于同源策略,在ajax调用中指定端口将不起作用。这意味着URL必须与脚本所在的服务器具有相同的域和端口 另外,请注意,这个问题已经被问过了,并且是在谷歌搜索的第一个结果之一-