Because of the presence of special characters in the string, it is impossible to pass them using JSON

Currently, I am utilizing this method to transmit data to the server through a GET request.

var val = {
    name: "abcd",
    age: "21"
}
var val2 = "test2";
http://server-name/getdata.htm?data=JSON.stringify(val)&data1=val2 

.

Everything is functioning correctly; however, issues arise when the value of val.name contains special characters such as "abcd&def," which causes the format of the request to distort due to the inclusion of "&".

How can I address this problem effectively?

Answer №1

What should be my next step?

The recommended action is to properly encode the elements of the query string using a secure method like encodeURIComponent. This function is universally supported by all browsers, not just Firefox.

While actual code examples were not provided, following this general format is advisable:

var link = "http://server-name/getdata.htm?data=" +
           encodeURIComponent(JSON.stringify(val)) +
           "&data1=" +
           encodeURIComponent(val2);

In cases where it is essential to encode key values such as data and data1, the optimal approach would be to employ encoding for keys in addition to their corresponding values:

var link = "http://server-name/getdata.htm?" +
           encodeURIComponent("data") + "=" +
           encodeURIComponent(JSON.stringify(val)) +
           "&" + encodeURIComponent("data1") + "=" +
           encodeURIComponent(val2);

However, if dealing with fixed keys where the encoded form retains its original value (as seen with data and data1), omitting key encoding may also suffice.

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

The form is not registering keypress events

Currently facing an issue with this piece of code: <form> <div class="searchBarDiv"> <input class="searchBar" id="search" type="search" placeholder="Search" onkeypress="search();"> </div> </form> The search(); fun ...

In absence of javascript, AJAX comes to the rescue for retrieving data

Is there a way to handle an AJAX get request if the browser does not have JavaScript enabled? Imagine clicking on an image thumbnail that leads you to a controller returning image details and a zoomed-in photo. I aim to use AJAX to display the enlarged p ...

PHP - Sending a JSON response from a class

I am working with a class: class A { protected $name; public function getName() { return $this->name . " example"; } public function setName($name) { $this->name = $name; } } When I run the following code: $r ...

Receive Real-Time Notifications -> Update Title Using an Array Retrieved from a JSON File

I've been working on updating a live chart every 5 seconds with new data from the database. While I could easily update the information, I encountered a problem when trying to set a path within the chart options for tooltips callbacks afterTitle. Spec ...

How can I translate these JQuery functions into vanilla JavaScript?

I am currently in the process of building a configurator using transparent images. The image configurator functions through a basic JQuery function: I have assigned each input element a data attribute called data-Image. This function identifies the data-Im ...

Searching for a string using JSONPath

I am facing a challenge with a massive JSON file containing over 20000 objects. My goal is to query a specific string within this json file. However, the issue lies in the fact that using jsonpath only allows for exact matches? Imagine my file structure a ...

Generating a hierarchical structure in Angular by converting a flat array into a Tree array

I am faced with the challenge of creating a tree Array that can be used in the primeng tree component. However, I am receiving a flat array from the backend. Here is an example of the data I receive: {name: 'teste', previousName: 'fathernam ...

Why do flex justifyContent and alignItems not have an effect on child <View/> components in React Native?

I am encountering an issue in React Native where a child view inside another view is not centering in the middle of the page. However, if the child view is not nested inside the parent view and stands alone, it does center properly in the middle of the pag ...

Problems Arising with Javascript Animation Functionality

I've created a script for an interactive "reel" that moves up or down when clicking on specific arrow buttons. However, I'm encountering two issues: 1) The up arrow causes it to move downward, while the down arrow moves it upward. 2) After exe ...

Is there a way to apply an event function after adding elements through append?

When I click the button, a div is appended inside the body. However, I am trying to make it so that when I click on this newly appended div, an alert message pops up. I have tried implementing this with a highlighted area, but have been unsuccessful. How c ...

The attempt to access https://registry.npmjs.org/yargs was unsuccessful

I am experiencing difficulties in downloading packages from npm. When attempting to download from https://registry.npmjs.org/yargs, I am receiving an error message that states: write EPROTO 101057795:error:14077419:SSL routines:SSL23_GET_SERVER_HELLO:tls ...

Dynamically change the state based on intricate layers of objects and arrays

Here is the issue I am facing I am attempting to update the state of an object named singleCarers I have the index of the roster in the rosters array that I need to update However, the key monday: needs to be dynamic, as well as the keys start: and fini ...

Is there a quicker method than using document.getElementById('element-id').innerHTML to retrieve content?

Currently, I am developing a user-friendly step-by-step wizard for my website to gather information about custom orders. Utilizing JavaScript, I have been tasked with updating the content of each "page" using the document.getElementById('element-id&ap ...

Send a JsonArray via Ajax

Struggling with posting a JSON Array to SENDGRID using Ajax. Postman works fine, but encountering error (bad request = missing parameters) in my .js file. Any assistance would be greatly appreciated. Note: The values are valid, sensitive information has ...

Tips for exporting data to a JSON file using the Google Play Scraper in NodeJS

Recently, I've been exploring the Google Play Scraper and I'm in need of a complete list of all appIds using NodeJS. The issue I'm facing is that it only outputs to console.log. What I really require is to save this output to JSON and then c ...

Leveraging JSON Schema If-Then-Else for Altering Property Data Types

Exploring the amazing capabilities of if-then-else keywords in JSON Schema 07 has been an eye-opening experience for me. I am intrigued by the idea of altering the type of a property based on the response to another property. My goal is to achieve someth ...

An issue arises when attempting to utilize URL parameters within a loader

In my React project, I am utilizing React-Router. The code for my movie page is as follows: import React from "react"; import axios from 'axios' export async function loader({ params }) { const movieId = params.movieId; const mov ...

Make a POST request using AJAX to the current page and retrieve a value using the $_POST method

I've been trying to solve this puzzle, but I can't seem to figure out what's going wrong... Here's the situation: I'm passing a value to a function and using AJAX to send this value via a POST request to the same page. Upon succes ...

Ways to verify if a specific extjs panel has finished loading

After a specific panel has finished loading, I need to insert a JavaScript code (using the panel's ID). What is the best way to ensure that the panel has been fully rendered so that I can access its ID using document.getElementById? Thank you. ...

How callbacks inside a loop can leverage Javascript closures

How can I ensure that the last line of code in the loop returns a value? $scope.runActionwithObjects = function() { for (var i = 0; i < $scope.Objects.length; i++) { console.log($scope.Objects[i]); //$scope is being acces ...