GithubHelp home page GithubHelp logo

isabella232 / wpcom-xhr-request Goto Github PK

View Code? Open in Web Editor NEW

This project forked from automattic/wpcom-xhr-request

0.0 0.0 0.0 295 KB

REST API requests to WordPress.com via XMLHttpRequest (and CORS)

Home Page: https://automattic.github.io/wpcom-xhr-request

License: MIT License

Makefile 2.91% JavaScript 97.09%

wpcom-xhr-request's Introduction

wpcom-xhr-request

REST-API and WP-API requests via XMLHttpRequest (and CORS)

CircleCI

You likely want to use the high-level APIs in wpcom.js instead of using this module directly.

Works in both the browser and Node.js via superagent.

Installation

Install wpcom-xhr-request using npm:

$ npm install wpcom-xhr-request

Example

<html>
  <body>
    <script src="wpcom-xhr-request.js"></script>
    <script>
      WPCOM.xhr('/me', function(err, data) {
        if (err) throw err;

        var div = document.createElement('div');
        div.innerHTML = 'Your WordPress.com "username" is: <b>@' + data.username + '<\/b>';
        document.body.appendChild(div);
      });
    </script>
  </body>
</html>

API

wpcomXhrRequest( [params], fn )

Params: optional parameters

  • method: GET as default.
  • apiNamespace: WP-API namepsace.
  • apiVersion: REST-API app version - 1 as default.
  • proxyOrigin: https://public-api.wordpress.com as default.
  • authToken: token authentication.
  • query: object used to pass the query to the request.
  • body: object used to pass the body to the request.
  • form-data: POST FormData (for multipart/form-data, usually a file upload).
  • processResponseInEnvelopeMode: default TRUE.

fn: request callback function

This function has three parameters:

  • error: defined if the request fails
  • body: the object body of the response
  • headers: the headers of the response
import handler from `wpcom-xhr-request`;

// get .com blog data usign `REST-API`
handler( '/sites/en.blog.wordpress.com', ( error, body, headers ) => {
  if ( error ) {
    return console.error( 'Request failed: ', error );
  }
  
  console.log( 'WordPress blog: ', body );
} );

// get .com blog data using `WP-API`
handler( {
  path: '/sites/en.blog.wordpress.com',
  apiNamespace: 'wp/v2'
}, ( error, body, headers ) => {
  if ( error ) {
    return console.error( 'Request failed: ', error );
  }
  
  console.log( 'WordPress blog: ', body );
} );

// get .org blog data (`WP-API`)
handler( {
  proxyOrigin: 'http://myblog.org/wp-json',
  path: '/',
  apiNamespace: 'wp/v2'
}, ( error, body, headers ) => {
  if ( error ) {
    return console.error( 'Request failed: ', error );
  }
  
  console.log( 'WordPress blog: ', body );
} );

Authentication

For API requests that require authentication to WordPress.com, you must pass in an OAuth token as the authToken parameter in the params object for the API call.

You can get an OAuth token server-side through node-wpcom-oauth, or any other OAuth2 interaction mechanism.

Tests

make test
make test-watch

License

MIT โ€“ Copyright Automattic 2014

wpcom-xhr-request's People

Contributors

tootallnate avatar retrofox avatar sirbrillig avatar timmyc avatar jsnajdr avatar nylen avatar ockham avatar gziolo avatar delan avatar dmsnell avatar youknowriad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.