AngularJS $ http get request encodes dates incorrectly inside object

Please review this issue.

When I try to send an object (which contains dates as properties) via $ http (type GET) - I get a URL that contains ":".

For example an object:

var a = {date:"15:36", name:"Test"}

      

I expect to get something like "site.com?date=15%3A36&name=test"

But what I actually get is: "site.com?date=15:36&name=test"

If I understand correctly, the reason is that: https://github.com/angular/angular.js/blob/master/src/ng/http.js

Line 50:

parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(value)));
In my example - it is object, not array. First of all - it is serializeing value. (serializeValue(value))

      

Line 13-18

function serializeValue(v) {
  if (isObject(v)) {
    return isDate(v) ? v.toISOString() : toJson(v);
  }
  return v;
}

      

It is an object, but it is not a date (it contains a date as a property). So we get the json string.

After that, this is the encoding data for the URI request.

I found it here: github.com/ angular / angular.js / blob / 720012eab6fef5e075a1d6876dd2e508c8e95b73 / src / ngResource / resource.js

(lines 405-411) (failed to add 2 links, excuse me).

 function encodeUriQuery(val, pctEncodeSpaces) {
    return encodeURIComponent(val).
      replace(/%40/gi, '@').
      replace(/%3A/gi, ':').
      replace(/%24/g, '$').
      replace(/%2C/gi, ',').
      replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
  }

      

It encodes the input data encodeURIComponent

, but after he returns values @

, :

, $

, ,

.

Is this an AngularJS bug or did I do something wrong? I am using AngularJS version 1.5.6. Please let me know your words or ideas.

+3


source to share


1 answer


Use $ httpParamSerializerJQLike Link

Example:



$http({
    url: URL,
      method: 'GET',
      params:  { date:"140:30", number:"10" },
      paramSerializer: '$httpParamSerializerJQLike',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
      }
})

      

+1


source







All Articles