Dealing with special characters in a json XMLHttpRequest: A guide

I'm currently grappling with the best approach for handling special or foreign characters within an AJAX request.

My current test code is as follows:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","test.json",true);
xmlhttp.setRequestHeader("Content-type","application/json; charset=UTF-8");
xmlhttp.send('{"test1":"æøå"}');

Upon receiving this content on the server (IBM Domino), it appears as REQUEST_CONTENT="{"test1":"├ª├©├Ñ"}". How should these strange characters be decoded?

One potential solution could involve using encodeURIComponent() on the value before sending the request. Is this the recommended approach, or are there more effective methods available?

Answer №1

It appears that you are referring to a Web Agent. In this case, the response you are receiving is as expected from the agent.

REQUEST_CONTENT remains untranslated when read, displaying raw UTF-8 in the string. Decoding it manually is necessary to interpret the data accurately.

To ensure character integrity, a safe approach is to URLEncode before transmitting and then URLDecode upon receipt of the data.

Alternatively, utilizing the JSON services components in XPages can streamline this process.

Extlib OpenNTF offers a sample database showcasing extlib JSON services. The hosted DB can be found here (hosting provider may vary).

Another potential resource is the Domino Data Service (DDS), which facilitates direct access to views/documents in JSON format.

Personally, out of these options, I recommend the XPages solution for better data management and control.

Answer №2

What programming language is your agent coded in? If it's Java, you can utilize the getBytes method to transform the REQUEST_CONTENT data into a byte array, and then simply initialize it as a new UTF-8 string. Here's an example:

byte[] requestContentBytes = requestContentString.getBytes();
String requestContentUTF8 = new new new String(requestContentBytes,"UTF-8");

However, I'm uncertain about the getBytes() function. It might be necessary to provide a charset argument for it. Without an argument, it will likely use the default charset of the platform to convert the String value to bytes. If the operating system the agent runs on shares the same default charset as the one you are viewing the string on, then this approach should be correct. If a charset argument is indeed required, the key point is that it should not be UTF-8 -- otherwise, utilizing the UTF-8 argument in the new String method would have no effect.

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

Div takes on the opacity of its parent element

I want to implement a modal popup using the Ajaxtoolkit's modalpopupextender, and so far it's working perfectly. While the popup is displayed, I need to dim out the rest of the page. To achieve this, I applied the modalPopup CSS class to the pop ...

Unnecessary PHP code will run on its own in JavaScript

Attempting to initiate a session in PHP and assign a value to a session variable within a Javascript function on a PHP/HTML page is creating an issue. The problem arises when the PHP code inside the if statement runs automatically upon loading the page, ra ...

Is there a way to capture the stdout and stderr output from a WebAssembly module that has been generated using Emscripten in JavaScript?

In my C++ code snippet below: #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } I compile the code using: emcc -s ENVIRONMENT=shell -s WASM=1 -s MODULARIZE=1 main.cpp -o main.js This c ...

Concealing an element by using parentElement.getElementsByClassName to set the display property to none

I am attempting to hide a button that generates a new element upon being clicked (to then be replaced by a delete button), but I'm struggling to set the display property to "none" when trying to target the same element using parentElement and then ret ...

Implementing dynamic loading of data in d3.js for retrieving the following node in a tree structure

I'm currently exploring the potential of using this D3 example (http://bl.ocks.org/mbostock/1093025) for an expanding tree structure. Most examples I've come across involve loading all data from a single JSON dataset. However, I'm encounter ...

Store Form Input as JSON Data File

Seeking advice on the optimal method to save submitted form data to a separate file called data.json, for future reference. The form layout is quite basic: <form> <fieldset> <label for="name">Name:</label> &l ...

Converting objects into JSON format

I'm trying to transform an object that looks like this: { user[id]: 1, user[name]: 'Lorem', money: '15.00' } Into the following structure: { user: { id: 1, name: 'Lorem', }, money: ...

Displaying minute scale from right to left using jQuery technology

I successfully created a minute scale that is functioning well, but now I am encountering an issue with displaying my scale. Instead of being oriented left to right, I would like it to be displayed from right to left. Can anyone suggest what might be wron ...

Deciphering a Base64 document from a JSON reply on the user's end: What's the process?

My project involves rendering a file on the server side, where I pass it as a "base64" string via json. I am now faced with the task of decoding and downloading this file on the client side. Below is a simplified version of the code that is relevant to my ...

Unable to successfully cancel requestAnimationRequest in a React component when using hooks

I'm currently working on a progress bar project where I aim to halt the animation (using cancelAnimationRequest) once it reaches a specific value (10, 100, ... N) and reset it back to 0. Unfortunately, in my existing code, although it resets to 0, th ...

What is the best way to include a title and a close button at the top of a menu, before the first item, when a button menu is clicked in material-ui?

One of the challenges I'm facing is adding a title to an icon button menu that contains a checkbox list of menu items. When the user clicks on the icon and opens the dropdown, I want the title to appear at the top of the dropdown along with a close bu ...

Focus on the original control that triggered a jQuery AJAX POST during a postback event

function pageLoad() { $(".VoteUp").live("click", function () { var Id = $(this).attr("index"); d = JSON.stringify({ "Id": Id }) $.ajax({ type: 'POST', url: '../API ...

What steps do I need to take to successfully integrate Font Awesome 5 with React?

Here is an interesting scenario: the initial icon is displayed, but it fails to update when the class changes. const Circle = ({ filled, onClick }) => { const className = filled ? 'fas fa-circle' : 'far fa-circle'; return ( ...

The children of the KendoUI treeview are showing up as null

Within my KendoUI treeview setup, the main nodes trigger a call to an MVC controller that checks for a nullable ID parameter before utilizing a different model. When accessing the URL: http://localhost:2949/Report/GetReportGroupAssignments The resulting ...

Having trouble retrieving the keyword property within a Vue.js promise

Struggling with an async validation process in Vue.js where I need to globally access the $axios instance, but encountering failures Validator.extend('async_validate_job_type', { getMessage: field => `The Name already exists`, val ...

Error: The context does not have a definition for React

import './App.css'; import ComponentC from './components/ComponentC'; export const UserContext = React.createContext() function App() { return ( <div className="App"> <UserContext.Provider value={& ...

Discover the nearest class and smoothly expand it with the slideDown function in jQuery

Hey there, I'm working on implementing a "View More" button on my page. The button will reveal another unordered list that currently has the class of "hidden-list". When the user clicks on "View More", I want it to slideToggle the hidden-list element ...

Using getJSON to return key/value pair from local host URL in JSFiddle - A step-by-step guide

After following a tutorial on building an API using Python, Flask, SQLite, and SQLAlchemy, I have successfully tested the connection by hitting the localhost address in my browser. Now, I am curious if it is possible to test this connection and see the des ...

AngularJS - Organize Item Hierarchy with Separate Containers for Each Group

My goal is to incorporate a $scope variable within an AngularJS controller that has the following structure: $scope.hierarchy = { name: 'bob', selected: true, children: [ { name: 'frank' }, { name: 'spike' ...

Can you guide me on how to export a named function using module.exports?

I have a script file for my discord bot that includes a specific command and a function with parsing logic that I want to reuse in my main index.js // File: ./commands/scrumPrompt.js // The function const extractDeets = function (f, scrum) { let items ...