cal-smith / ajax.js

A little ajax library
MIT License
33 stars 3 forks source link

Vars are not appended to the request body when using POST #9

Open oculus42 opened 9 years ago

oculus42 commented 9 years ago

During POST, the variables should be URL encoded information placed in the request body, not appended to the URL. It also requires specific headers:

"Content-type": "application/x-www-form-urlencoded "Content-length": ...

Additional details. http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

cal-smith commented 9 years ago

Reading the spec it seems that the headers should be set automatically? - https://fetch.spec.whatwg.org/#concept-bodyinit-extract

I've already cooked up a solution for attaching POST variables however, so that much is done.

oculus42 commented 9 years ago

You are correct that it will default to application/x-www-form-urlencoded. There are other encodings which you may want/need to support, though they are less common in AJAX. Would you want multi-part for file uploads?

Off the top of my head, I don't recall the details for all the other methods.

You linked the spec for fetch rather than XHR. https://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

cal-smith commented 9 years ago

IIRC the "living standard" is now specifying XMLHttpRequest in terms of fetch, which explains how I got there. That being said it's probably better to stick closer to XHR.

It should be possible to support multi-part/formdata/blobs without too much issue, but I'll probably have to add a method or two - I'd like to keep the library from "guessing" what you're doing, but some data inference may be necessary.

Edit: I've done a preliminary fix as part of commit a1f76192343c021371309806bba5e0c436ea9b1f

cal-smith commented 9 years ago

So I did some testing and reading up - Any URL can include paramaters (url.com?a=1&b=2), but POST can include data in the request body as well (for forms etc). The solution I have arrived at is adding a .data() method that allows setting the request body. At the moment it just supports form-urlencoded type data, but multipart and formdata should be trivial to implement.

cal-smith commented 9 years ago

Fix for 1.0.1 is in 72121099e48539c37dd527667d743c995193375f I'll target additional functionality for 1.1