简体   繁体   English

JQuery发布到Tomcat Servlet

[英]JQuery post to a Tomcat Servlet

I am trying to make a JQuery $.post to a Java Servlet. 我正在尝试将JQuery $.post为Java Servlet。 I integrated the Tomcat server into Apache and if the Tomcat server is on the same machine as the Apache the $.post succeded. 我将Tomcat服务器集成到Apache中,如果Tomcat服务器与Apache在同一台机器上,则$.post成功。 (The Java Servlet receives it). (Java Servlet接收它)。

If the Tomcat servlet is on a remote machine and if I make $.post(http://ip:8080/App/MyServlet,...) the servlet doesn't receive anything. 如果Tomcat servlet在远程计算机上并且如果我生成$.post(http://ip:8080/App/MyServlet,...)则servlet不会收到任何内容。

If I make a JQuery $.post on my machine I have like this $.post(Myservlet,.....) . 如果我在我的机器上制作一个JQuery $.post ,我就喜欢这个$.post(Myservlet,.....) If I try like this : $.post(http://localhost:8080/App/MyServlet,...) it doesn't work. 如果我尝试这样: $.post(http://localhost:8080/App/MyServlet,...)它不起作用。

How should I make a JQuery $.post to a remote uri? 我应该如何为远程uri创建一个JQuery $.post

How should the remote uri for a Tomcat Servlet look like? Tomcat Servlet的远程uri应该如何?

Thanks, 谢谢,

Jquery runs in the browser (client-side), which means it's subject to the browser's same-origin policy, which is a good thing. Jquery在浏览器(客户端)中运行,这意味着它受浏览器的同源策略的约束,这是一件好事。

This means ajax requests that are GET or POST can only be made to the domain of the page making the ajax request. 这意味着只能对发出ajax请求的页面域进行GET或POST的ajax请求。

There are 2 ways to bypass the policy. 有两种方法可以绕过该政策。 The first is to have the remote server vouch for the request, the second is to sneak around the browser's same-origin policy. 第一个是让远程服务器担保请求,第二个是偷偷摸摸浏览器的同源策略。

So if you have control over the remote server, or if the admin who does takes requests to open the server/domain to foriegn ajax requests, then the server just needs to send the following header: 因此,如果您可以控制远程服务器,或者如果管理员确实接受打开服务器/域的请求以发出ajax请求,那么服务器只需要发送以下标头:

Access-Control-Allow-Origin: your-local-domain.org

The browser gets back the response header, sees that the requesting page is in the above list, and allows the response through. 浏览器返回响应头,看到请求页面在上面的列表中,并允许响应通过。

If you have no control over the remote server, here are the sneakier ways to get around same-origin policy: 如果您无法控制远程服务器,则可以采用以下方法来解决同源策略:

  1. Make an ajax request to a local url with the parameters, and have it pass it along to the servlet, and the have that proxy script return whatever the servlet responds with. 使用参数向本地URL发出ajax请求,并将其传递给servlet,并让该代理脚本返回servlet响应的任何内容。

  2. JSONP (which I'm still fuzzy on, honestly, but jquery's ajax documentation goes into it) JSONP(老实说,我仍然很模糊,但是jquery的ajax文档会进入它)

  3. Script injection, where you leverage the fact that the script element's src is not limited by the same-origin policy. 脚本注入,您可以利用脚本元素的src不受同源策略限制的事实。

Of the 3, I think the first is the safest, least hackish, and most honest (so to speak), but JSONP has become the simple and easy way to pull of a cross-domain request in jquery. 在3中,我认为第一个是最安全,最不诚实,最诚实(可以这么说),但JSONP已成为在jquery中提取跨域请求的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM