PHP Classes

File: src/utils/ajax.js

Recommend this page to a friend!
  Classes of Maniruzzaman Akash   WP Emailer   src/utils/ajax.js   Download  
File: src/utils/ajax.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: WP Emailer
Allow WordPress users to configure email settings
Author: By
Last change:
Date: 1 year ago
Size: 1,514 bytes
 

Contents

Class file image Download
/** * Internal dependencies. */ const $ = jQuery; /** * Get AJAX request. * * @param {String} params Request params. * @returns Promise */ export async function getRequest(params) { const url = `${wpEmailer.ajax_url}?_wpnonce=${wpEmailer.nonce}&${params}`; return $.ajax({ url, type : "GET", processData: false, contentType: false, }); } /** * Post AJAX request. * * @param {Object} body Post object * @returns Promise */ export async function postRequest(body) { const postData = { _wpnonce: wpEmailer.nonce, ...body }; return $.ajax({ type : "POST", url : wpEmailer.ajax_url, data : generateFormDataFromObject(postData), processData: false, contentType: false, }); } /** * Generate Form Data from Object * * @param object object data * * @return Object FormData Object */ const generateFormDataFromObject = (object) => { let formData = new FormData(); buildFormData(formData, object); return formData; }; const buildFormData = (formData, data, parentKey) => { if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File) ) { Object.keys(data).forEach(key => { buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key); }); } else { let value = data == null ? '' : data; formData.append(parentKey, value); } };