【问题标题】:Jquery communication with local serverJquery 与本地服务器的通信
【发布时间】:2011-04-16 22:41:45
【问题描述】:

所以我试图通过 jquery post 方法与本地 java 服务器进行通信,但我运气不佳。我使用的邮政编码是:

    $.post('localhost:5051/receive', {'plugs':client['plugins']});

其中 client['plugins'] 是一个具有确认值的字符串。

我试图接收这个的 servlet 是(所有必要的导入):

public class BaseServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public BaseServlet() {
        super();
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String plugins = request.getParameter("plugs");
        System.out.println("HERE");
        System.out.println(plugins);
    }
}

这个 servlet 似乎没有从 post 方法接收任何通信。

请注意,javascript 是在我刚刚在浏览器中打开的本地 html 文件上运行的。

以下是包含所有必要导入的基本服务器代码:

public class MyServer {

    public static void main(String[] args) throws Exception {
         Server server = new Server();
        Connector connector = new SocketConnector();
    connector.setPort(5051);
    server.setConnectors(new Connector[]{connector});

    ServletHandler handler=new ServletHandler();
    server.setHandler(handler);

    handler.addServletWithMapping("BaseServlet", "/receive");

    server.start();
    server.join();
    }
}

谢谢,如果这很直观,我深表歉意。

【问题讨论】:

    标签: javascript jquery servlets


    【解决方案1】:

    当您说$.post('localhost:5051/receive' 时,您可能是指$.post('http://localhost:5051/receive',但这仅在您已经在使用该主机和端口时才有效(出于安全原因,same origin policy 对此进行了限制),因此您不妨使用相对 URI 和说$.post('/receive'

    编辑。我刚刚发现:

    请注意,javascript 是在我刚刚在浏览器中打开的本地 html 文件上运行的。

    正如我之前所说,运行 JS 的页面必须位于 the same host and port 作为向您请求数据的 URI。

    【讨论】:

    • 感谢您的帮助,但不幸的是,这并没有解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2015-11-01
    • 2021-03-15
    • 1970-01-01
    相关资源
    最近更新 更多