Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Request Issue #115

Closed
barisyild opened this issue Feb 20, 2019 · 5 comments
Closed

Post Request Issue #115

barisyild opened this issue Feb 20, 2019 · 5 comments

Comments

@barisyild
Copy link

barisyild commented Feb 20, 2019

swup.loadPage({
     url: `/upload.php`,
     method: "POST",
     data: {simplePost: true, _token: "secretToken"},
});

This post is sending '[object Object]' data.

I tried parse object as url variable but content-type variable is still 'text/plain;charset=UTF-8'

@gmrchk
Copy link
Member

gmrchk commented Feb 21, 2019

The data is actually not a plain object, but rather a FormData object.

This page will give you more info on creating or sending the FormData. swup uses the following to send forms.

let form = event.target; // the form element
let formData  = new FormData(form); 

@dougkeeling
Copy link

dougkeeling commented Feb 22, 2019

I am having a similar issue when trying to send GET data. I was happy to find this post and see that the plain objects aren't supported, but I'm still having problems with the following code:

data = {topic: "test"};

var formData = new FormData();
for(var key in data) {
    formData.append(key, data[key]);
}

swup.loadPage({ 
    url: "/search",
    method: "GET",
    data: formData,
});

If I understand things correctly, my FormData var should be set up appropriately, however, the GET string is never appended to the URL. I would expect to see /search?topic=test but instead I just get /search.

@gmrchk
Copy link
Member

gmrchk commented Feb 22, 2019

I understand your confusion because I've been there some time ago. When sending the GET request, the URL of the request is what matters, not the request data, which makes sense when you think about how the GET requests work...

However, I haven't found any "native" way to serialize the form into the URL format, and so, swup builds the URL manually, before appending it to the value defined in form action attribute and sending the request.

If you do find some better way, please, let me know. :)

@dougkeeling
Copy link

@gmrchk Thanks for that link to the code! Looks like it's just building the string then doing a regular loadPage.

So — correct me if I'm wrong — there's really no reason for me to even bother trying to build the FormData for a GET request, right? If I have all the data I need to send, I should just build the string myself and do:

swup.loadPage({ url: "/search?topic=test" });

@gmrchk
Copy link
Member

gmrchk commented Feb 25, 2019

Yep. @dougkeeling
Can we close this? @barisyild

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants