remote form
v0.4.3
允许通过 AJAX 提交表单的功能。
如果表单方法是GET
,并且对于所有其他方法都是FormData
,则该函数将使用window.fetch
发出基于表单的请求,并将有效负载编码为 URL 参数。
请求对象在回调函数中可用,允许在发送请求之前修改标头和正文。
$ npm install --save @github/remote-form
import { remoteForm } from '@github/remote-form'
// Make all forms that have the `data-remote` attribute a remote form.
remoteForm ( 'form[data-remote]' , async function ( form , wants , request ) {
// Before we start the request
form . classList . remove ( 'has-error' )
form . classList . add ( 'is-loading' )
let response
try {
response = await wants . html ( )
} catch ( error ) {
// If the request errored, we'll set the error state and return.
form . classList . remove ( 'is-loading' )
form . classList . add ( 'has-error' )
return
}
// If the request succeeded we can do something with the results.
form . classList . remove ( 'is-loading' )
form . querySelector ( '.results' ) . innerHTML = response . html
} )
< form action =" /signup " method =" post " data-remote >
< label for =" username " > Username </ label >
< input id =" username " type =" text " >
< label for =" password " > Username </ label >
< input id =" password " type =" password " >
< button type =" submit " > Log in </ button >
< div class =" results " > </ div >
</ form >
没有原生自定义元素支持的浏览器需要一个polyfill。
npm install
npm test
根据麻省理工学院许可分发。有关详细信息,请参阅许可证。