在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kilianc/koa-jsonp开源软件地址:https://github.com/kilianc/koa-jsonp开源编程语言:JavaScript 100.0%开源软件介绍:koa-jsonpA koajs streaming friendly JSONP middleware that supports GET/POST JSONP requests. Install
Exampleconst db = require('nano')('http://localhost:5984/my_db')
const stringify = require('json-array-stream')
const jsonp = require('koa-jsonp')
app.use(jsonp())
app.use(mount('/users', function *() {
this.type = 'json'
this.body = db.view('koa_example', 'users')
.pipe(JSONStream.parse('rows.*.value'))
.pipe(stringify())
}))
app.use(mount('/dow', function *() {
this.body = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
}))
app.listen(8080) yield
APIfunction jsonp (options) Returns the generator middleware. Options
Use cases
The middleware auto selects the right format based on the presence of callback (querystring) and the HTTP method. JSONP CORSThis is not required if your frontend and your API run in the same domain or if you set the There is a way to read a JSONP response after sending a POST request (form) in the browser. The solution is to set an iframe as target of the form (could be hidden). The iframe will load your response page, in this case plain HTML with a script tag and will call your This is an old snippet using var __requestsMap__ = {}
function post(url, fields, callback) {
var requestId = new Date().getTime()
var inputs = []
Object.keys(fields).forEach(function (fieldName) {
inputs.push(new Element('input', {
name: fieldName,
value: data[fieldName]
}))
})
if (url.match(/\?/)) {
url += '&callback=__requestsMap__["' + requestId + '"]'
} else {
url += '?callback=__requestsMap__["' + requestId + '"]'
}
var form = new Element('form', {
'enctype': enctype,
'method': 'post',
'action': url,
'target': requestId,
'style': 'display: none'
}).adopt(inputs).inject(document.body)
var iframe = new Element('iframe', {
id: requestId,
name: requestId,
styles: { display: 'none' }
}).inject(document.body)
__requestsMap__[requestId] = function (response) {
callback(null, response)
// cleanup
delete __requestsMap__[requestId]
iframe.dispose().destroy()
form.dispose().destroy()
}
form.submit()
} How to contributekoa-jsonp follows (more or less) the Felix's Node.js Style Guide, your contribution must be consistent with this style. The test suite is written on top of mochajs/mocha and it took hours of hard work. Please use the tests to check if your contribution is breaking some part of the library and add new tests for each new feature.
and for your test coverage
LicenseThis software is released under the MIT license cited below.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论