Giter Club home page Giter Club logo

najax's Introduction

najax

Travis CI Dependency Status devDependency Status js-standard-style

jQuery ajax-stye http requests in node

jQuery ajax is stupid simple. This project provides a lightweight wrapper for the nodejs http request object that enables jquery ajax style syntax when making serverside requests to other webpages in node.js

In addition to najax.get, , handles ssl and makes some reasonable assumptions based on inputs and everything can be overridden by passing an options object.

Getting Started

Install the module with: npm install najax

var najax = $ = require('najax')
$.get('http://www.google.com', callback)
najax('http://www.google.com', { type: 'POST' }, callback)
najax({ url: 'http://www.google.com', type: 'POST', success: callback })
najax({ url: 'http://www.google.com', type: 'POST' })
  .success(callback)
  .error(errorHandler)

$.get, $.post, $.put, $.delete...

Run unit tests

  • npm install && npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using standardjs.

najax's People

Contributors

alanclarke avatar avindra avatar danmcclain avatar demersus avatar doublerebel avatar jardilio avatar kvas-damian avatar mar-tino avatar mixu avatar philippmi avatar philschatz avatar pwfisher avatar rctay avatar rek avatar rhildred avatar spike008t avatar tmcdonnell87 avatar turbo87 avatar vontio avatar wololodev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

najax's Issues

Publish `master` to `npm`

The latest version on npm is out of date with what is on master by a couple of years. The latest is marked as 0.1.5 and currently discards headers. It looks like there has been a lot more of activity since then and those updates would be nice to use.

Support options.type

$.ajax supports a 'type' parameter as an alias for method (http://api.jquery.com/jquery.ajax/). Najax should support this, as some frameworks (e.g. Backbone) generate ajax options with this as the parameter name.

It's incredibly easy: just add
options.method = options.method || options.type;
to the beginning of the request function.

Awesome module - thanks for the help!

lodash dep is 4.6M

4.6M    /app/server/node_modules/najax/node_modules/lodash

Does najax really need to include all of lodash? Can we just include the parts we need? This is very heavy for "nano" ajax.

najax: method jqXHR getAllResponseHeaders not implemented

We just moved a project onto a new box, and I'm getting this error:

najax: method jqXHR getAllResponseHeaders not implemented

Looking at the source, I can see the notImplemented fn is first assigned to jqXHR.getAllResponseHeaders and then that is being assigned with the real getAllResponseHeaders fn:

    dfd.getAllResponseHeaders = jqXHR.getAllResponseHeaders = function getAllResponseHeaders () {
      var headers = []
      for (var key in res.headers) {
        headers.push(key + ': ' + res.headers[key])
      }
      return headers.join('\n')
    }

I'm not clear on what the scenario is when the notImplemented method would be called, since it looks like it is always being overwritten inside the request callback. Can someone clarify for me?

I don't have much insight on the new box this project is deployed on, I'm guessing that its unable to make outside requests - but wanted to understand this specific error before making that assumption.

Thanks!

Error when uploading PDF/binary file

I use your great plugin to upload PDF files to a server. Internally the data seems to be transformed to UTF-8. I have found 2 places where you add utf-8. At first when building the request header near line 68.
The second place is when the data is actually written, near line 207.
I have changed these as follows:

} else {
        /* set data content type */
        if (o.contentType === 'application/pdf') {
            o.headers = _.extend({
                'Content-Type': o.contentType,
                'Content-Length': o.data.length
            }, o.headers)

        }
        else {
            o.headers = _.extend({
                'Content-Type': o.contentType + ';charset=utf-8',
                'Content-Length': Buffer.byteLength(o.data)
            }, o.headers)

        }

and
// SEND DATA
if (o.method !== 'GET' && o.data) {
if (o.contentType === 'application/pdf') {
req.write(o.data, 'binary');
}
else {
req.write(o.data, 'utf-8');
}
}

This is a quick solution for PDF only. There should be a solution to handle all binary data, maybe an additional option or a list of all known minetypes.

Array of objects differs from jQuery

najax:

/api/x?affiliations[][schoolId]=1234&affiliations[][endYear]=2020&affiliations[][startYear]=2016&affiliations[][schoolId]=5678&affiliations[][endYear]=2014&affiliations[][startYear]=2011

jQuery:

/api/x?affiliations[0][schoolId]=1234&affiliations[0][endYear]=2020&affiliations[0][startYear]=2016&affiliations[1][schoolId]=5678&affiliations[1][endYear]=2014&affiliations[1][startYear]=2011

The indexes are missing from the arrays, causing an interesting interpretation in Express:

{
  affiliations: [
    { schoolId: 1234 },
    { endYear: 2020 },
    { startYear: 2016 },
    { schoolId: 5678 },
    { endYear: 2014 },
    { startYear: 2011 }
  ]
}

instead of:

{
  affiliations: [
    { schoolId: 1234, endYear: 2020, startYear: 2016 },
    { schoolId: 5678, endYear: 2014, startYear: 2011 }
  ]
}

options.type should default to GET if not specified

type should default to GET if not specified.

To demonstrate, the following will log an error:

ajax({
  url : 'http://www.google.com/search',
  data : {
    q : 'Latest proposals for esnext'
  },
  success:function(res) {
    console.log('html is', res);
  },
  error:function(e) {
    console.log('error is ', e.responseText)
  },
})

while the following will log the HTML response from google:

ajax({
  url : 'http://www.google.com/search',
  type : 'GET',
  data : {
    q : 'Latest proposals for esnext'
  },
  success:function(res) {
    console.log('html is', res);
  },
  error:function(e) {
    console.log('error is ', e.responseText)
  },
})

The only difference is that one specifies type : 'GET',

Async option support

$.ajax suports an option called async that does what the name suggests. It seems that najax doesn't support it. Right?

Won't post nested objects

Excellent plugin. Had an issue with POST. Would not pass a nested 'data' object when using "application/x-www-form-urlencoded".

Example:
var d = {

action: 'ajaxNode',
type: 'post',
request: 'get_posts_by_user',
data:{ID:'1',post_type:'notes'},

};

najax({
url: ajaxURL,
type: 'POST',
data: d
},function(res){
console.log(res);
});

d.data wouldn't get posted. Only a string will post.

I swapped module 'querystring' with 'qs' and removed line 97:
o.data = o.data+'\n';

Fixed the issue.

charset=utf-8 set on every non GET request

Hi,

I'm using https://github.com/ember-fastboot/ember-cli-fastboot, which uses najax to work and I have an API server that handles JSON API requests. The thing is that najax is sending the charset parameter on the Content-type header of every non GET request.

The JSON API spec doesn't allow any parameters on that header explicitly: http://jsonapi.org/format/#content-negotiation-servers.

Looking at the code, the parameter is added to header regardless of the content type itself which doesn't seem to be correct: https://github.com/najaxjs/najax/blob/master/lib/najax.js#L69.

Use native promises

jQuery 3.0.0 release uses A+ spec promises, which have been native in nodejs land for some time

@doublerebel what do you think about getting rid of $.deferred dependency and perhaps even lodash tbh

CORS doesnt seem to be supported yet

When I make a request for a resource the requires CORS, it looks like the CORS headers cannot be accessed. I get see this response:

najax: method jqXHR."getAllResponseHeaders" not implemented
Trace
    at Object.getAllResponseHeaders (/Users/hassan/codez/ember-fastboot-deploy/node_modules/najax/lib/najax.js:102:15)
    at ClientRequest.errorHandler (/Users/hassan/codez/ember-fastboot-deploy/node_modules/najax/lib/najax.js:119:34)
    at ClientRequest.emit (events.js:107:17)
    at TLSSocket.socketErrorListener (_http_client.js:272:9)
    at TLSSocket.emit (events.js:129:20)
    at TLSSocket.<anonymous> (_tls_wrap.js:940:18)
    at TLSSocket.emit (events.js:104:17)
    at TLSSocket._finishInit (_tls_wrap.js:458:8)

I 'm guessing that https://github.com/najaxjs/najax/blob/master/lib/najax.js#L118 needs to be implemented in order to support CORS?

npm version outdated

Hello,

Thanks for this lib! Would you mind updating the npm package with the latest updates from Github?

Cheers,

Charles

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.