Skip to content
Advertisement

Process inputted data for JS Fetch Api request like jQuery does in Ajax request when processData is true

First of all. Code of ajax_handler.php

JavaScript

I have jQuery Ajax function with Promise wrapper like this.

JavaScript

Responses (Respectively)

JavaScript

See, the variation in submitted data as params

I tried to convert this function to JS Fetch Api.

JavaScript

Responses (Respectively)

JavaScript

Problem:

Obviously, I can’t process and send inputted data as jQuery is doing because of processData: true,. jQuery seem to handle data efficiently for both POST as well as GET.

I can’t to figure out a way to replicate working of processData functionality of jQuery Ajax as the params have lots of variation and I need some generic code to process inputted data for Fetch as jQuery does.

I want to do this in JS only without third party libraries like jQuery.

Advertisement

Answer

I did mention that I only want the solution in JS but as commented by @CBore that I have few options in this matter.

  1. Write my own recursive function.
  2. Use URLSearchParams as described here. But this is not working for my scenario as the params are not just simple object.

JavaScript
  1. Use jQuery.param. But I do not want to use a full library just for a single function / advantage.
  2. Use light-weight library like jquery-param.

So in the end, I have settled for jquery-param for the time being.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement