angular post method remove character +

1

I do not really know what is happening, but when I send a form with an angle to the API that I have to enter it into the database if I put it in a string, the '+' character changes it to a space.

 //Metodo para crear la vivienda
 crearVivienda(vivienda : Vivienda): Promise<any>{
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });

var body = "UR="+vivienda.UR+"&num_promocion="+vivienda.num_promocion+"&Direccion="+vivienda.direccion+";

console.log(body);

return this.http.post(viviendasURLcreate, body, options)
  .toPromise()
  .then()
  .catch(this.handleError);

}

I have tried to send the form by postman and introduce it well. How would I have to make the request to not exchange the '+' character for a space ''?

Thanks

    
asked by Jose Yeste 27.12.2017 в 09:17
source

1 answer

1

This is usually a common problem and is often very rare at first, since the + is used by the URL to separate two words. To be able to use it as such, you must code the values before adding them to the URL. In JS and TypeScript there is encodeURI()

This is how you use encodeURI() :

let encodedName = encodeURI('xyz+axis');
let encodedURI = 'http://localhost:3000/page?name='+encodedName;

//.. O también con string interpolation
let encodedURI = 'http://localhost:3000/page?name=${ encodedName }';

In the same way you could decode it, if necessary.

let decodedVar = decodeURI(encodedVar);

Additional:

  

More information on coding in W3SChools

    
answered by 27.12.2017 / 12:59
source