reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Permit a different dataFilter function depending to ajax request

Open paztis opened this issue 11 years ago • 3 comments

It's graet that a global "dataFilter" function can be unsed for all ajax requests. But I want to run a special for some requests. Is it possible to also pass a "dataFiler" function in request options to only inpact the assiociated resquest ?

Thanks

paztis avatar Sep 24 '14 10:09 paztis

what if the global filter gave you a URL? You could use it like this...

$.ajaxSetup({
    dataFilter: function (response, type, url) {
      if (url.match(/^\/api\//)) return someThingCustom(response)
      return response
    }
  })

ded avatar Sep 24 '14 20:09 ded

@paztis would that work for you if it was implemented?

ded avatar Sep 24 '14 20:09 ded

The problem for me is I've got a lot of requests to manage and a lot of different dataFilters. If everything is manage in the same function, a lot of "if" will append. It's not good for the perfs...

My real problem is my responses are JSON so I specify the type to JSON. But before parsing it, I must change a IP pattern in the raw response. If I keep text response instead of JSON, I lose the simplicity of reqwest (I must parse by myself).

In jQuery, dataFilter is an option parameter for each request. It was great for me.

That's why I propose to have a global "dataFilter" function , and potentialy one "dataFilter" function in request option, like this:

reqwest({ url: 'path/to/html', method: 'post', data: { foo: 'bar', baz: 100 }, dataFilter: function(rawResponse, type) { return rawResponse.replace("{{FOO}}", "{{BAR}}"; } });

Do you see what I mean ?

paztis avatar Sep 24 '14 21:09 paztis