Preserve the newline character within a string in JavaScript

One of my API methods returns an RSA key in the following format:

"publickey" : "-----BEGIN PUBLIC KEY-----\nMIGfMA0G
CSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5WleAVeW5gySd
QVFkTi44q1cE\ncWDT2gmcv2mHcWhwI/9YqGV2LqpMASe
4t4XS/88fvTTHafHn1KaL9F7d73T9k4cp\nm+YxKJexkK
/wOxf/NZBieoADaLLaU1+OoPauSw3i4DZxKqIm1nxNHSV
LUfyY44gN\nClVxtZzW/nwdWEdSQwIDAQAB\n-----END PUBLIC KEY-----\n"

However, when I receive this public key and store it in a variable on the client side,

var key = data.publickey

The \n symbols are not present in the key variable, but they are necessary for proper document signing.

Is there a way to preserve the \n in the string?

Answer №1

Following Ben's suggestion, you will need to use double escape (\\). Therefore, the correct code would be:

\\n

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

Guide on how to transform a JSON response object into a specific format using Jackson or another library

When I receive a JSON response from a third party web service, it is structured like this: { "meta": { "code": 200, "requestId": "1" }, "response": { "locations": [ { "id": "1", ...

Encountering an error: [nsIWebProgressListener::onStatusChange] when utilizing jQuery AJAX within a click event?

Greetings! I am currently learning how to implement AJAX with jQuery to load an HTML document into a div element within another HTML document. Here is the approach I am using: function pageload() { $.ajax({ url: 'Marker.aspx', ...

Hover over two different divs with JQuery

I have a situation where I have two HTML table rows. When I hover over the first row, I want to display the second row. However, once the mouse leaves both rows, the second row should be hidden. Is there a way to achieve this using JQuery? <tr class=" ...

jQuery document.ready not triggering on second screen on Android device

Why is jQuery docment.ready not firing on the second screen, but working fine on the first/initial screen? I also have jQuery Mobile included in the html. Could jQuery Mobile be causing document.ready to not work? I've heard that we should use jQuery ...

creating keys dynamically in a Mongoose schema using Node.js JavaScript

I consider myself an intermediate in node.js. Within my mongoose schema, I have a variety of fields listed below: result_1 result_2 result_3 result_4 result_5 The data will come in numeric form (1-5) as the result number, and based on this number, I nee ...

Error: Node.js Express is unable to recognize the content type

I'm currently facing an issue while trying to transmit a file from a phonegap application to a nodejs server, specifically with regards to the content-type in the request headers. Error: unrecognized content-type: multipart/form-data;boundary express ...

What is the most effective way to organize an array according to a key function that is computationally intensive?

After posting this question regarding sorting an array with a key function, it became evident that utilizing a comparison function was inevitable. The challenge at hand includes: Having a resource-intensive key function that needs to be transformed into ...

What is the best way to transfer information from a view to a controller in Laravel using AJAX?

I am facing an issue with sending data from the view to the controller in Laravel version 7 I am sending data from a form and ul li elements I have an array of data in JavaScript Here is my HTML code: <ul name="ali" id="singleFieldTags&q ...

Transferring an MSAL token to the backend with the help of Axios

I encountered an issue while attempting to send the user ID, which was previously decoded from a JWT token along with user input data. The problem arises when I try to send the data and receive an exception in the backend indicating that the request array ...

The error message popped up indicating that JSON_encode in php is not supported

An issue has arisen, showing an error message as json_encode not supported by web server. Below is the snippet of code being affected: <?php require_once('db_login.php'); $con=mysql_connect($db_host,$db_user,$db_password); if(!$con) die ...

What is the process for inserting a scroll bar within a div element?

   I have recently created a webpage using some divs, along with a bit of CSS and JavaScript. I am struggling to figure out how to add a scrollbar to one of my divs. The code is not overly complex, as it includes both CSS and JavaScript. <html> & ...

Seeking advice on the most effective way to divide my JSON data

Can anyone provide guidance on how to extract and display information from a JSON array while excluding the initial course number? I am able to split the array, but need help in displaying only the course name. For example, if we consider the JSON data ava ...

What is the reason behind fullstack-angular generator utilizing Lo-Dash's merge rather than document.set?

This is the original code snippet used for updating: exports.update = function(req, res) { if(req.body._id) { delete req.body._id; } Thing.findById(req.params.id, function (err, thing) { if (err) { return handleError(res, err); } if(!thing) { ...

Live updating Google doughnut chart generated from real-time SQL query results

I am looking to create an interactive Google doughnut chart that displays data retrieved from a SQL query. Despite my efforts to find sample code, I have been unsuccessful in getting the chart to render on the page. Below is the script: <script type= ...

Guide on retrieving an ArrayList() from intricate function in Angular

Simplicity is the key to my question. Let's take a look at this Angular method: getAllOrdersHeaders(){ this.getAllOrdersIds().subscribe(idList=>{ idList.forEach(id=>{ this.ordersCollection.doc(id).collection('metadata&apo ...

Stop accidental clicking on objects in React-Fiber which are using Three.js

Check out this interactive cube made up of planes! An issue I've encountered is that clicking on a plane passes through to the ones behind it, rather than only registering a click on the plane directly under my mouse. Any suggestions for fixing this ...

Encountering a 500 error while attempting to pass HTML via an AJAX call to a C# MVC Controller

Currently facing a challenge that I need help with. I have multiple pages successfully making Ajax calls to my C# controllers, but now I'm trying to incorporate a simple content management feature. The goal is to update HTML stored in a database with ...

What is the reason behind receiving the error "PHP Warning: Trying to access property "nodeType" on null" when using this PHP AJAX search function?

I am working on developing a real-time search feature inspired by an example from w3schools, which can be found at this link: https://www.w3schools.com/php/php_ajax_livesearch.asp. My task involves searching through an xml file containing 1000 different it ...

Duplicate numerous Td elements

I am trying to manipulate a table with 3 columns and 6 rows by copying the contents of certain cells into a newly created column, inserting them in the middle of the table. Here is the desired outcome: https://i.sstatic.net/RVTfV.png This is the approach ...

What is the best way to transfer floating point input values from one page to another for later retrieval?

Is there a way to send floating point values to another page? I tried sending a floating point value in an AJAX request, but the receiving page only gets an integer value. <div class="form-group"> <label for="" class="col-sm-4 control-label"> ...