0

To summarize : all my requests with formData objects content work in Firefox and Chrome. Some of them work also in IE11 but one not. The strange thing is the requests are not significantly different.

I use this FormData object to send data to Struts2 by Json :

formData.append("JsonInput", angular.toJson(encodeURIComponent(JSON.stringify(myVariable))));

This request doesn't work in IE11 but works in Firefox and Chrome :

$http({
    method: 'POST',
    url: appServer + '/rcdCalSaveData',
    respondType: 'json',
    headers: { 'Content-Type': undefined },
    data : formData
})

Real variable names have been changed for the example.

The result in my Action method in the back controller is null (JsonInput).

Besides that, others FormData sent by requests work (not much different) :

formData.append("JsonAddVariable", angular.toJson(encodeURIComponent(JSON.stringify(anotherVariable))));

$http({
    method: 'POST',
    url: appServer + '/rcdDefAddfdkrfikef',
    respondType: 'json',
    headers: { 'Content-Type': undefined },
    data: formData,
    params: { mode: (service.modeAdd ? 1 : 0) }
})

So I think that the variable passed is the problem (Array in the first example and an Object in the second). But if I replace the first FormData by this it works as well :

var temp = "%5B%7B%22firstVar%22%3A7%2C%22secondVar%22%3A0%2C%22thirdVar%22%3A-2%7D%5D";
formData.append("JsonInput", temp);

Which corresponds to : [{"firstVar":7,"secondVar":0,"thirdVar":-2}]

So what Object variable content can cause problem with AngularJS and Struts2 ?

(I am using IE11)

Shahnawaz Hossan
  • 2,695
  • 2
  • 13
  • 24
mtnp
  • 314
  • 7
  • 24
  • It's not related to Struts2, but [this](https://stackoverflow.com/a/37090893/573032) is what you should know about how to use angularjs with Struts2. – Roman C Jul 20 '20 at 03:24
  • @RomanC My predecessors used formData everytime and it worked so this solution works as well. But in my case something is modifying behavior of the request. If it's not in Struts2 it's in AngularJS... – mtnp Jul 20 '20 at 11:30
  • In Struts2 when you use request form data is encoded and sent via http to the server. Then you can handle it with the action. Actions are interceptor aware and processing form data is happened before your code is executed. No need to perform serialization and deserialization form data during request because it's doing automatically. In your case it looks more like conversion scheme but without code. – Roman C Jul 27 '20 at 17:23
  • I observed that my requests did not contain the correct header when I inspect via browser : Content-type is set to plain/text instead of multipart/form-data. So I succeeded to change that but even with this trick Struts still getting null in the back controller. The documentation is hard to understand : how can I inspect with Struts, requests and action class behavior when transforming fields, before my method is triggered ? – mtnp Jul 28 '20 at 13:53
  • The method of action class is triggered as a result of the action execution. It should be configured externally. Interceptors are part of the action configuration. If you don't understand Struts2 basics you can't inspect a behaviour of the request which may flow several times before you get a result. Everything is explained in the docs. You can start reading from [this](https://stackoverflow.com/a/43733311/573032) answer. – Roman C Jul 29 '20 at 05:15
  • Ok thank you I will study that – mtnp Jul 29 '20 at 09:46
  • @RomanC I finally found that this answer fixed my problem : https://stackoverflow.com/a/2627539/5240905 I put a GET request just before the POST one and it works. But I cannot know why because authentication stuff never caused problems before. – mtnp Aug 24 '20 at 16:46
  • This is not a problem with authentication. Probably it's a poor backend implementation with *Struts2*. You should learn more [here](https://stackoverflow.com/a/59895899/573032) how to `POST` json objects to it. However still poorminded cannot get a solution. – Roman C Aug 25 '20 at 20:19
  • @RomanC Thanks, my solution works but it's ugly I will just use it temporarily. I will use your solution in the next deployment – mtnp Aug 26 '20 at 08:45

0 Answers0