我試圖更新公司對象,如下面的company.js中所示。 當我嘗試從company.js調用put方法,它給我的400狀態錯誤和執行不put方法在CompanyController.java進入。公司對象也可在$ scope中使用。 雖然company.js執行最後的網址是: http://localhost:8080/Jobkreisel/protected/company/50 但它甚至沒有在更新方法CompanyController.java進入,剛剛轉會到錯誤塊$ http.put(URL,$ scope.company,config)方法。PUT方法Spring MVC中的HTTP狀態400 - AngularJS
company.js
$scope.updateCompany = function (updateCompanyForm) {
if (!updateCompanyForm.$valid) {
$scope.displayValidationError = true;
return;
}
$scope.lastAction = 'update';
var url = '/Jobkreisel/protected/company/' + $scope.company.companyID;
var config = {};
alert("Company scope "+$scope.company.companyID);
alert("Company config "+config);
$http.put(url, $scope.company, config)
.success(function (data) {
alert('In update success');
})
.error(function(data, status, headers, config) {
console.debug(data);
alert('data:' + data);
alert('status: ' + status);
alert('update error');
});
};
CompanyController.java
@Controller
@RequestMapping(value = "/protected/company")
public class CompanyController extends UserBaseController {
@Autowired
private CompanyService companyService;
@RequestMapping(value = "/{companyID}", method = RequestMethod.PUT, produces = "application/json")
public ResponseEntity<?> update(@PathVariable("companyID") int companyId,
@RequestBody Company company,
Locale locale) {
if (companyId != company.getCompanyID()) {
return new ResponseEntity<String>("Bad Request", HttpStatus.BAD_REQUEST);
}
companyService.save(company);
return null;
}
}
請告訴我,爲什麼它沒有執行成功的塊。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
<!-- Spring servlet that will handle the requests-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring basic configurations -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring.xml
</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Enconding helper filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<servlet-name>dispatcher</servlet-name>
</filter-mapping>
<!-- Encoding utility -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
</web-app>
網絡日誌:
遠程地址:127.0.0.1:8080要求 網址:http://localhost:8080/Jobkreisel/protected/company/50請求 方法:PUT狀態代碼:400錯誤請求請求頭文查看源代碼 接受:application/json,text/plain,/ A ccept-Encoding:gzip, deflate,sdch Accept-Language:zh-CN,en; q = 0.8連接:keep-alive Content-Length:175內容類型:application/json; charset = UTF-8 Cookie: JSESSIONID = 1w425u610rioe Host:localhost:8080 Origin:http://localhost:8080 Referer:http://localhost:8080/Jobkreisel/protected/company User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64)爲AppleWebKit/537.36 (KHTML,例如Gecko)鉻/ 40.0.2214.115 Safari瀏覽器/ 537.36請求 淨荷視圖源
{companyID: 「50」,名稱: 「Agrident GmbH的」,網址: 「http://www.agrident.com/」, twitter:「」,...} ausbildungvideourl:「」 companyID:「50」employee:「」Facebook:「」name:「Agrident GmbH」 studiumvideourl:「」twitter:「」video:「」網站: 「http://www.agrident.com/」響應頭視圖源 的Content-Length:0附註:無緩存服務器:碼頭(6.1.21)
喜,你的servlet實際上映射到'web.xml'文件中的'/ Jobkreisel/*'上了嗎? – blint 2015-03-03 11:10:36
嗨,問題更新了,你可以檢查web.xml文件。 – user1534344 2015-03-03 12:51:09
你可以看看JS調試器(特別是「網絡」或「網絡」選項卡),看看Angular試圖發送的請求是什麼樣的? – 2015-03-03 12:55:26