[英]Angular2 does not pass parameters in POST request
I am trying to pass a parameter using Angular POST request to Tomcat server, Spring Framework. 我正在尝试使用Angular POST请求将参数传递给Tomcat服务器Spring Framework。 Somehow I see that the parameter is there when it is sent, but it somehow does not arrive/properly retrieved on the backend.
我以某种方式看到参数在发送时在那里,但是以某种方式没有到达/在后端正确检索。 Here is the Angular2 code:
这是Angular2代码:
addCompany() {
console.log("addCompany button clicked!");
console.log("company name: " + this.input);
let nameId = this.input;
let body = JSON.stringify({ input: nameId });
let headers = new Headers({ 'Content-Type': 'application/json', 'X-CSRF-TOKEN':this.getToken() });
console.log("csrf token: " + this.getToken());
let options = new RequestOptions({ headers: headers });
this.http.post('http://localhost:8080/views/addcompany', body, options).toPromise()
.then(() => {
console.log("company added!");
this.reloadList();
});;
}
When I am trying to get it in Spring I am getting null
for the parameter: 当我尝试在Spring中获取它时,该参数的
null
:
@RequestMapping(value = "/addcompany", method = RequestMethod.POST)
@ResponseBody
public void addCompany(HttpServletRequest request,
HttpServletResponse response) {
String nameId = request.getParameter("input");
eventService.addCompany(nameId);
}
I tried also this way: 我也这样尝试过:
@RequestMapping(value = "/addcompany", method = RequestMethod.POST)
@ResponseBody
public void addCompany(Model model, @RequestParam("input") String nameId) {
eventService.addCompany(nameId);
}
And in Angular code I have been trying to change commas everywhere, like: 在Angular代码中,我一直在尝试在各处更改逗号,例如:
let nameId = this.input;
let body = JSON.stringify({ 'input': nameId });
etc. 等等
I tried this one: Angular2 - Http POST request parameters 我尝试了这个: Angular2-Http POST请求参数
Following the suggestion JB Nizet
I tried to create POJO
: 按照建议
JB Nizet
我尝试创建POJO
:
public class Company {
public String input;
public Company() {
this.input = "";
}
public String getInput() {
return this.input;
}
public void setInput(String input) {
this.input = input;
}
}
Register it in my @Configuration
file: 将其注册到我的
@Configuration
文件中:
@Bean
public Company getCompany(){
return new Company();
}
And changed the request method to the following: 并将请求方法更改为以下内容:
@RequestMapping(value = "/addcompany", method = RequestMethod.POST)
@ResponseBody
public void addCompany(Company company) {
eventService.addCompany(company.input);
}
After that I am getting Company
object in the method with input=null
. 之后,我使用
input=null
在方法中获取Company
对象。
Then I tried to deregister the Company
@Bean
from @Configuration
and change the request method to the following: 然后,我尝试从
@Configuration
注销Company
@Bean
,并将请求方法更改为以下内容:
@RequestMapping(value = "/addcompany", method = RequestMethod.POST)
@ResponseBody
public void addCompany(@RequestBody Company company) {
eventService.addCompany(company.input);
}
But after that I am getting 415 (Unsupported Media Type)
error. 但是之后,我收到
415 (Unsupported Media Type)
错误。
In pom.xml
I have the following jackson import: 在
pom.xml
我具有以下jackson导入:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
Substituting it for second jackson version solved the issue: 将其替换为第二个杰克逊版本可以解决此问题:
<properties>
...
<jackson.version>2.7.5</jackson.version>
</properties>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
You're sending a JSON object as body, and you expect to get a request parameter containing an attribute of this JSON object. 您正在将一个JSON对象作为主体发送,并且希望获得一个包含此JSON对象属性的请求参数。
That won't happen. 那不会发生。 You need a POJO that matches with the structure of the JSON sent, take that POJO as argument and annotate it with
@RequestBody
. 您需要一个与发送的JSON结构匹配的POJO,以该POJO作为参数并使用
@RequestBody
对其进行注释。 Jackson will unmarshal the JSON to the POJO and the POJO will be passed to your method. Jackson会将JSON解组到POJO,然后POJO将传递给您的方法。
Request parameters can be used if the request contains an application/x-www-form-urlencoded payload: the kind of payload you send when submitting a HTML form, without doing any JavaScript. 如果请求包含application / x-www-form-urlencoded负载,则可以使用请求参数:您在提交HTML表单时不执行任何JavaScript便发送的负载类型。
Instead of let body = JSON.stringify({ input: nameId });
代替
let body = JSON.stringify({ input: nameId });
try let body = { input: nameId };
尝试
let body = { input: nameId };
Try to use : 尝试使用:
let body:string='input='+nameId;
And use this header 并使用此标题
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded', 'X-CSRF-TOKEN':this.getToken() });
Only for other readers if you want to send more than 1 parameter. 如果您要发送多个参数,则仅适用于其他阅读器。 use something & fro separating parameter .
使用来回分离参数。 like below code
像下面的代码
let body :string='username='+username+'&password='+password;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.