jQuery,ajax,端口号
问题描述:
如何从服务器获取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?
感谢
答
在Ajax调用指定端口将无法正常工作,由于同源策略(http://en.wikipedia.org/wiki/Same_origin_policy)。这意味着该URL必须与服务器具有相同的域和端口,其中托管脚本。
而且,请注意,这个问题已经被问过了,是在谷歌搜索时的第一批成果之一 - Is it possible to specify a port in a ajax call
http://*.com/questions/2099728/how-do-i-send -an-AJAX请求上-A-不同端口与 - jquery的 – Getz 2013-05-03 07:36:27