Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax从外部html页面调用tomcat服务器war url_Javascript_Java_Ajax_Spring_Tomcat - Fatal编程技术网

Javascript 使用ajax从外部html页面调用tomcat服务器war url

Javascript 使用ajax从外部html页面调用tomcat服务器war url,javascript,java,ajax,spring,tomcat,Javascript,Java,Ajax,Spring,Tomcat,我使用的是apache-tomcat-7.0.67,在webapps文件夹War name:下部署了一个jar: Account war是使用spring boot创建的,因此没有显式定义web.xml,但是url映射是使用@RequestMapping(value=“/submitForm”,method=RequestMethod.POST)”定义的 当我从邮递员或JMeter点击这个url时,就像http://localhost:8080/Account/submitForm请求到达服务

我使用的是apache-tomcat-7.0.67,在
webapps
文件夹War name:下部署了一个jar:

Account
war是使用spring boot创建的,因此没有显式定义web.xml,但是url映射是使用
@RequestMapping(value=“/submitForm”,method=RequestMethod.POST)”定义的
当我从邮递员JMeter点击这个url时,就像
http://localhost:8080/Account/submitForm
请求到达服务器并返回响应
现在我有了另一个文件夹,其中只包含html文件,这些文件与战争无关

它有一些html文件,其中包含使用类似javascript的ajax调用

$.ajax({
                type: 'POST',
                url: '/Account/submitForm',
                 success: function (resp) {
                 console.log(resp);
                },
                error: function(e) {
                console.log('Error: ',e);
                }  
            });

但是,即使在使用url:
”之后,请求也不会到达服务器https://localhost:8080/Account/submitForm“
,有人能告诉我怎么解决这个问题吗?我完全被卡住了。

也许你不应该用你的战争的名字

我在这里使用这样的请求

$.ajax( ... url: "../GUI/ResponseTime", ...
在我的课堂上有这样的注解

@WebServlet(name = "ResponseTime", urlPatterns = {"/GUI/ResponseTime"})

无论文件的war名称如何,它都可以工作。实际问题是javascripts跨域源策略,它不允许从war文件之外的静态html文件调用任何ajax。我通过添加

<filter>
 <filter-name>CorsFilter</filter-name>
 <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
 <filter-name>CorsFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>`

克斯菲尔特
org.apache.catalina.filters.CorsFilter
克斯菲尔特
/*
`

在tomcat的
web.xml
文件中,该文件允许跨域原始调用。spring还支持跨源,我在这里的一个好链接中发现了这一点
这是来自W3C的规范您如何知道请求“从未到达服务器”?将文件放在
webapps/html
中意味着它们是从
/Account
上下文中的servlet上下文(“web app”)提供的。我从Firebug的网络面板(其中显示状态
Aborted
)了解到,很抱歉,我没有得到您所说的
意味着它们是从servlet上下文(“web app”)提供的从/Account context
中,请详细说明它不会按上下文名称进行映射,实际问题是javascript的跨域(CORS)策略,它不允许从我的war之外的html文件调用任何url。无论如何,谢谢你的建议