我实现了一个包含记录和表单的网格,以便创建和更新这些记录。编辑记录并在之后保存记录时,客户端将执行以下操作:
var form = this.getView().getForm();
var record = form.getRecord();
record.beginEdit();
var updatedRecord = form.getValues();
record.set(updatedRecord);
record.endEdit();
通过这样做,客户端将向Tomcat服务器发送PUT请求,并在主体内部更新参数。
tomcat服务器允许web.xml
:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
当我使用浏览器插件发送HTTP请求时,我们可以在此响应中看到一切正常:
Status Code: 200 OK
Allow: PUT
Content-Language: en
Content-Length: 190
Content-Type: application/json
Date: Mon, 22 May 2017 12:21:40 GMT
Server: Apache-Coyote/1.1
但是当ExtJS发送PUT请求时,我得到以下响应:
HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://localhost:1337
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Access-Control-Allow-Origin,Access-Control-Allow-Credentials
Allow: DELETE,GET,OPTIONS
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1056
Date: Mon, 22 May 2017 11:55:37 GMT
如果ExtJS发送PUT请求,为什么tomcat不允许PUT,GET和POST。 当ExtJS发送POST请求时,一切正常,我得到以下响应:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:1337
Access-Control-Expose-Headers:Access-Control-Allow-Origin,Access-Control-Allow-Credentials
Content-Length:225
Content-Type:application/json
Date:Mon, 22 May 2017 12:59:07 GMT
Server:Apache-Coyote/1.1
我需要做些什么才能让Tomcat允许PUT消息?
答案 0 :(得分:0)
您必须在EXTJS中的headers
商店配置中指定proxy
。
proxy: {
headers: {'Content-Type': "application/json" }
...
}
我认为通过指定标题可以解决问题。
我认为actionMethod
也可能会产生问题。
默认extjs's store
发送GET
请求,但如果您希望它发送PUT
请求,则可以在actionMethods
config
proxy: {
actionMethods: {
read : 'PUT',
create : 'POST',
update : 'PUT',
destroy : 'DELETE'
}
...
}
请参阅link。