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
根據麻省理工學院許可分發。有關詳細信息,請參閱許可證。