0

I'm passing an object using the jQuery $.post method. When it's loaded using the $.get method, I need that the message field of the object is parsed correctly. I was able to remove the equal "=" sign and the "&" sign, but if it's a long message it will contain the plus "+" sign and the commas are not displayed correctly. Here is the console output i get:

{user: "demo", message: "Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipisci…+qui+officia+deserunt+mollit+anim+id+est+laborum."}
message: "Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit%2C+sed+do+eiusmod+tempor+incididunt+ut+labore+et+dolore+magna+aliqua.+Ut+enim+ad+minim+veniam%2C+quis+nostrud+exercitation+ullamco+laboris+nisi+ut+aliquip+ex+ea+commodo+consequat.+Duis+aute+irure+dolor+in+reprehenderit+in+voluptate+velit+esse+cillum+dolore+eu+fugiat+nulla+pariatur.+Excepteur+sint+occaecat+cupidatat+non+proident%2C+sunt+in+culpa+qui+officia+deserunt+mollit+anim+id+est+laborum."
user: "demo"
__proto__: Object

The commas are replaced by the %2C character and the spaces are replaced from the plus sign. How to obtain the text without this signs?

here is a function I write for this scope, but it's not working at all.

function parseData(data){
 var params = {}
 params.data = data.split("&");
 params.result = {};
  for(var i = 0; i < params.data.length; i++) {
   var item = params.data[i].split("=");
   params.result[item[0]] = item[1];
  }
 return params.result;
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Oshione
  • 88
  • 13
  • Possible duplicate of [Decoding URL parameters with JavaScript](https://stackoverflow.com/questions/12042592/decoding-url-parameters-with-javascript) – Ravi Jul 27 '19 at 05:19

1 Answers1

1

Use this one :

function parseData(data){
   return decodeURIComponent((data + '').replace(/\+/g, '%20'));
}

Use this function hope it will work for you :

export function parseData(data) {
    url = decodeURI(url);
    if (typeof url === 'string') {
        let params = url.split('?');
        let eachParamsArr = params[1].split('&');
        let obj = {};
        if (eachParamsArr && eachParamsArr.length) {
            eachParamsArr.map(param => {
                let keyValuePair = param.split('=')
                let key = keyValuePair[0];
                let value = keyValuePair[1];
                obj[key] = value;
            })
        }
        return obj;
    }
}
Piyush Shukla
  • 244
  • 1
  • 11
  • I need also to remove the `&` and the `=` from the object returned from ajax, your suggested code will work also with the commas that now are replaced with %2C ? – Oshione Jul 27 '19 at 05:40
  • So I will expect as result of the function the original string right? – Oshione Jul 27 '19 at 05:43
  • I've tested you code suggestion, but it will not work. The data passed to the function are not processed and the string will be displayed in this way: `param=xx&param2=xxx+yyyy` ! – Oshione Jul 27 '19 at 11:18
  • 1
    **Use this function hope it will solved your problem :** `export function parseData(data) { url = decodeURI(url); if (typeof url === 'string') { let params = url.split('?'); let eachParamsArr = params[1].split('&'); let obj = {}; if (eachParamsArr && eachParamsArr.length) { eachParamsArr.map(param => { let keyValuePair = param.split('=') let key = keyValuePair[0]; let value = keyValuePair[1]; obj[key] = value; }) } return obj; } }` – Piyush Shukla Jul 29 '19 at 12:00