Messy code appeared when sending an AJAX post to JBoss EAP 7 without using encodeURIComponent

Initially, the project functions smoothly on tomcat using UTF-8 and jboss eap 6 with UTF-8 page encoding as well. Additionally, the jboss configuration includes:

<servlet-container name="default" default-buffer-cache="default" stack-trace-on-error="local-only" default-encoding="UTF-8">

Furthermore, when utilizing encodeURIComponent or serialize, it operates effectively. However, there is a failure when using serialize and adding certain parameters as shown below:

  var params = $("#form_viewOrUpdateSchedule").serialize();
  params += "&recordId=" + recordId;
  params += "&custName=" + custName;
  params += "&startTime=" + startTime;
  params += "&content="
+ content;
  params += "&endTime=" + endTime;
  params += "&scheduleDate=" + getScheduleDate();
  bodymask();
  $.ajax({
    url: url,
    type: "POST",
    data: params,
    datatype: "JSON",
contentType : "application/x-www-form-urlencoded; charset=utf-8",
    success: function(data) {

Moreover, it performs well when using serialize or similar to the following:

  var recordId = $("#schedule_list_id").val();
  var custName = $("#schedule_list_custName option:selected").val();
  var startTime = $("#schedule_list_startTime").val();
  var endTime = $("#schedule_list_endTime").val();
  var content = $("#schedule_list_content").val();
  var startIndex = $("#schedule_list_startTime").get(0).selectedIndex;
  var endIndex = $("#schedule_list_endTime").get(0).selectedIndex;
  if (startIndex > endIndex) {
    alertInfo("提示", "结束时间不能早于开始时间");
    return;
  }
  var params = {}
  params.recordId = recordId;
  params.custName = custName;
  params.startTime = startTime;
  params.content = content;
  params.endTime = endTime;
  params.scheduleDate = getScheduleDate();
  $.ajax({
    url: url,
    type: "POST",
    data: params,

In addition, jboss eap 7 (now known as WildFly, version 10) relies on undertow instead of tomcat.

Subsequently, a similar issue was raised in , and the difficulty of setting the default character encoding was resolved in WildFly 8.0, mentioned in

Lastly, the question arises as to why the code works seamlessly on tomcat but fails on jboss eap 7 with the aforementioned "thirdly" JavaScript. How can this problem be resolved without modifying the JavaScript code?

Answer №1

Make sure to configure the URI encoding for your http-listener and any other listeners you have set up.

For instance:

<http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true" url-charset="UTF-8"/>

You can also use the CLI:

/subsystem=undertow/server=default:server/http-listener=default:write-attribute(name="uri-charset", value="UTF-8")

Remember to apply this configuration to all relevant listeners.

It's worth noting that system properties starting with org.apache.catalina.* are specific to Tomcat. In WildFly 8+ and EAP7+, Undertow is used as the web server instead of JBossWeb based on Tomcat, so these properties will not impact your setup.

Answer №2

After delving into the source code of both Tomcat and WildFly, I finally uncovered why the application functioned perfectly in Tomcat but encountered issues in WildFly.

In WildFly, a crucial difference was observed in

io.undertow.server.handlers.form.FormEncodedDataDefinition.FormEncodedDataParser.doParse(final StreamSourceChannel channel)
. The code snippet revealed:

buffer.clear();
int c = channel.read(buffer);
if (c > 0) {
   buffer.flip();
   while (buffer.hasRemaining()) {
       byte n = buffer.get();
       builder.append((char) n);//the issue arose here, [a][b][c] would be transformed to [0][a][0][b][0][c]

On the other hand, when examining Tomcat, details within

org.apache.tomcat.util.http.Parameters.processParameters(byte bytes[], int start, int len,Charset charset)
shed light on another approach.

This time,

new String(byte[] bytes, int offset, int length, Charset charset)
was utilized to convert the post content from byte[] to String.

The divergence lies in the default encoding being utf-8 without js URIComponentEncoding in Tomcat, allowing it to operate effectively in such scenarios.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we determine the total character count of a file that has been loaded into a textarea

I have a textarea where I can count the number of characters as I type. function calculateCharacters(obj){ document.getElementById('numberCount').innerHTML = obj.value.length; } <textarea name="textField" id="my_textarea" class="text_edit ...

What exactly does a 'hoisted manifest' mean when it comes to using Yarn?

Encountering an issue while attempting to install a package using yarn, I am receiving the error message: expected hoisted manifest for \"myPackage#@material-ui/core#react-dom\" However, the concept of a 'hoisted manifest' is not entir ...

Unable to utilize Bower due to a node.js malfunction

Currently facing an issue while attempting to utilize bower for installing all necessary components for my website project. Each time I make an attempt, the following error presents itself: TypeError: Object #<Object> has no method 'toLowerCase ...

Uncaught TypeError: Cannot create new instances of User - Node.js server not recognizing User as a constructor

I encountered an issue while attempting to save a user to MongoDB database using a post request. The error message "TypeError: User is not a constructor" keeps popping up despite the straightforward setup of my code. I have double-checked but can't se ...

Loop through the elements of a class in JavaScript and choose all except for the one that was

Imagine having 5 div elements, each with a similar onclick-function that hides the other divs when clicked. HTML: <div id="1" class="divs" onclick="hide()"></div> <div id="2" class="divs" onclick="hide()"></div> <div id="3" cla ...

Updating a jQuery variable through an AJAX request

Is there a way to update a variable that is located outside of an AJAX call from an event that is inside this AJAX call? This snippet of code shows that the var units is initially set to metric, but when a selectbox changes (inside the AJAX call), I want t ...

Duplicate Key Error in MongoDB

I am currently developing a service that enables multiple events to store data on MongoDB. Each event creates new collections on MongoDB when it occurs, and if the same event needs to store different data, a new document in MongoDB is created. Below is th ...

React's UseEffect can cause an endless loop of renders, leading to the error message "Maximum update

My Cart component contains an array of Cards. Whenever a user removes a product from the cart, it is removed from both the local storage and the UI. import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom&a ...

What is the best way to retrieve the second to last element in a list

When using Protractor, you have convenient methods like .first() and .last() on the ElementArrayFinder: var elements = element.all(by.css(".myclass")); elements.last(); elements.first(); But what about retrieving the element that comes right before the ...

The screen-responsive navigation bar is experiencing functionality issues

I am facing an issue with my navigation bar on both desktop and mobile. When I maximize the window while the mobile navbar is open, it disappears as expected but the desktop navbar does not appear. I am using a bootstrap template and I am unsure if solving ...

Animation will cease once the page is refreshed via Ajax

I'm facing a challenge with a problem that I can't seem to resolve. The issue lies in my use of this plugin for displaying random images. The only step left is to refresh the content within the div. However, every time I attempt to do so, the an ...

Error: The variable is not declared in the AJAX/JQuery code

I'm encountering an issue with my code that involves grabbing HTML from an AJAX request. I am trying to hide one row of a table and show another based on the response, but for some reason, the variable holding the HTML content seems to disappear when ...

Load an XML file from the local server asynchronously on the Chrome web browser

Attempting to load a local XML/XSL file into a variable for editing seems to be causing an issue. The code provided functions properly in IE and Chrome, however, Chrome displays a warning due to the synchronous nature of the call. function loadXMLDoc(fileN ...

Angular displaying a blank screen, even though the complete dataset is available

I am currently working on my first website using Angular and I've encountered a problem. When I click on 'view project', it should return the data specific to that item. The strange thing is, when I log my JavaScript console, I can see all t ...

Is there a way to capture the input from the text box and store it in the local storage?

I'm confused about why the data entered into the input box is not being saved to local storage. <body> <input id="name"> <button onclick="bob()"> save </button> </body> <script> const user = document.getElementByI ...

What is the best way to divide multiple event handlers in jQuery?

Is there a way to organize the code below more effectively by splitting the multiple events into separate lines? $document.find('body').on( 'click dblclick mousedown mousemove mouseout mouseover mouseup mousewheel keydown keypress keyup ...

Difficulty encountered when transferring an array from Xcode to JavaScript via SBJSON

In an attempt to transfer an array from Xcode to a local HTML file, I am utilizing jQuery in the HTML code. This involves using loadHTMLString: with baseURL to read both the HTML and included .js files. However, I am encountering an issue when trying to us ...

Is it possible to retrieve all data stored in AsyncStorage using React Native, while excluding the

In my current implementation, I am utilizing AsyncStorage.setItem() to store a string key and JSON object in AsyncStorage. For example: However, upon retrieving data from AsyncStorage using getAllKeys() and multiGet(), it has become apparent that I only n ...

Display the date in the user interface grid table

I've integrated ui-grid in my project to showcase data along with dates. An example of a date fetched from an ajax call would be: DateReview='06/25/2016' The current format being mm/dd/yyyy for display purposes. The column definition in ...

Counting numbers and displaying results using JavaScript with a JSON string

Looking at this JSON string { "ResultSet": { "version": "1.0", "Error": 0, "ErrorMessage": "No error", "Locale": "us_US", "Quality": 40, "Found": 2, "Results": [{ "quality": 72, ...