T
T
TostPOST2020-05-07 23:17:55
PHP
TostPOST, 2020-05-07 23:17:55

Why empty $_POST on fetch() request?

I want to implement background sync, for this, during requests to save any text, requests do not go to the server, but are stored in indexedDB, then during the sync event, the service worker runs through all objects in IndexedDB and sends requests to the server, the point is that I have all requests through Ajax, I’m not used to working with fetch, that is, I somehow adapted those data for ajax requests for fetch. As a result, when requesting the server, there is no data, $_POST is empty, php://input = "[object Object]", although Content-Type: 'x-www-form-urlencoded'.
Here is the service worker code:

self.addEventListener('sync',function(event){
    if(event.tag == 'requestsync'){
        request = indexedDB.open('requests');
        request.onsuccess = function(event){
            var db = event.target.result,storage,transaction,data;
            transaction = db.transaction('requeststorage','readonly');
            storage = transaction.objectStore('requeststorage');
            data = storage.getAll();
            data.onsuccess = function(event){
                data = event.target.result;
                for(key in data){
                    object = data[key];
                    console.log(object);
                    transaction = db.transaction('requeststorage','readwrite');
                    storage = transaction.objectStore('requeststorage');
                    delrequest = storage.delete(object.id);
                    delrequest.onsuccess = function(event){
                        console.log('Deleted request');
                    }
                    var settings = {
                        method: object.options.type,
                        mode:'cors',
                        headers: object.options.headers ? object.options.headers : {'Content-Type':'application/x-www-form-urlencoded'},
                        cache:'no-cache',
                        credentials:'include',
                        redirect:'follow',
                        body: object.data
                    };
                    fetch(object.options.url,settings).then(function(response){    
                        response.json();
                    })
                    .then(function(body){
                        console.log(body);
                    })
                }
            }
        }
        request.onupgradeneeded = function(event){
            var db = event.target.result;
            if(!db.objectStoreNames.contains('requeststorage')){
                db.createObjectStore('requeststorage',{keyPath:'id',autoIncrement:true});
            }
        }
    }
});

At the same time, the code itself performs its entire task, i.e. settings contains all the necessary data.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-05-08
@bingo347

You can put a string, FormData, Blob or ArrayBuffer in the body field, but apparently you put a regular object. You need to add a transformation.
Well, it’s worth a better understanding of promises, in particular

fetch(object.options.url,settings).then(function(response){    
                        response.json();
                    })
                    .then(function(body){
                        console.log(body); // здесь всегда будет undefined, независимо от ответа
                    })
think why so
PS
var db = event.target.result,storage,transaction,data;
I'd cut my hands off...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question