thank_you
thank_you

Reputation: 11107

& Causes String to Split When Sent to Server

I am sending a string to the server using ng-resource. If I add & into the string at any point, the characters after & are not sent to the server.

For example if I send the string to the server:

"This part of the string is shown & this part of the string is NOT shown"

everything after & is never shown. It's as if it's been chopped before being sent to the server.

Here is a working code sample.

angular.module('testApp', ['ngResource'])

.service('TestService', testService)

.controller('Controller', theController);

function testService() {
  this.$get = get;

  get.$inject = ['$resource'];

  function get( $resource ) {
    var baseUrl = window.location['origin'];

    var theResource = $resource(
      baseUrl + '/test_url',
      {},
      {
        testMe: {method: 'GET', url: '/test_url'}
       }
    )

    return theResource;
  }
}

theController.$inject = ["TestService"];

function theController(TestService) {
  activate();

  function activate() {
    var stringToSend = "this part of the string is shown & this part of the string will NOT be shown";

    // The server will only see the text "this part of the string is shown &"
    TestService.testMe({stringParam: stringToSend}, function(resp) {});
  }
}

What's wrong and how do I fix this?

PS: When I mean not shown, I mean it's as if that part of the string was never sent.

Upvotes: 0

Views: 61

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

Do encode the string before send it to server using encodeURI method. I think decoding of parameter value will happen automatically.

Code

TestService.testMe({
  stringParam: encodeURI(stringToSend)
}, function(resp) {});

Upvotes: 1

Related Questions