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

docs/httpPost #2163

Merged
merged 5 commits into from
Sep 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions src/io/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,79 @@ p5.prototype.httpGet = function () {
*
* @method httpPost
* @param {String} path name of the file or url to load
* @param {String} [datatype] "json", "jsonp", "xml", or "text"
* @param {String} [datatype] "json", "jsonp", "xml", or "text".
* If omitted, httpPost() will guess.
* @param {Object} [data] param data passed sent with request
* @param {function} [callback] function to be executed after
* httpGet() completes, data is passed in
* httpPost() completes, data is passed in
* as first argument
* @param {function} [errorCallback] function to be executed if
* there is an error, response is passed
* in as first argument
*
* @example
* <div>
* <code>
* // Examples use jsonplaceholder.typicode.com for a Mock Data API
*
* var url = 'https://jsonplaceholder.typicode.com/posts';
* var postData = { userId: 1, title: 'p5 Clicked!', body: 'p5.js is way cool.' };
*
* function setup() {
* createCanvas(800, 800);
* }
*
* function mousePressed() {
* // Pick new random color values
* var r = random(255);
* var g = random(255);
* var b = random(255);
*
* httpPost(url, 'json',
* postData,
* function (result) {
* strokeWeight(2);
* stroke(r, g, b);
* fill(r, g, b, 127);
* ellipse(mouseX, mouseY, 200, 200);
* text(result.body, mouseX, mouseY);
* });
* }
* </code>
* </div>
*
*
* <div><code>
*
* var url = 'https://invalidURL'; // A bad URL that will cause errors
* var postData = { title: 'p5 Clicked!', body: 'p5.js is way cool.' };
*
* function setup() {
* createCanvas(800, 800);
* }
*
* function mousePressed() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add a note about p5.dom ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This form of mousePressed is bundled with p5.js, only p5.Element.mousePressed is bundled with p5.dom.js so the user probably won't need p5.dom.js when running this example on their own.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - new here. Thanks!

* // Pick new random color values
* var r = random(255);
* var g = random(255);
* var b = random(255);
*
* httpPost(url, 'json',
* postData,
* function (result) {
* // ... won't be called
* },
* function (error) {
* strokeWeight(2);
* stroke(r, g, b);
* fill(r, g, b, 127);
* text(error.toString(), mouseX, mouseY);
* });
* }
*
* </code>
* </div>
*
*/
p5.prototype.httpPost = function () {
var args = Array.prototype.slice.call(arguments);
Expand Down