2014-01-27 88 views
2

從他們的官方文檔:香菜AJAX發送POST變量

 Custom ajax validation made simple. 
parsley-remote-method="POST" to change default GET call. 
parsley-remote-datatype="jsonp" if you make cross domain ajax call and expect jsonp 
Parsley will accept these valid returns with a 200 response code: 1, true, { "success": "..." } and assume false otherwise 
You can show frontend server-side specific error messages by returning { "error": "your custom message" } or { "message": "your custom message" } 

但是如果我想送一個變量,POST請求我能做些什麼?

+0

我想如果你把一個名字屬性放在你的輸入中,一個名字相同的變量和輸入值將與發送請求一起發送。 – MamaWalter

+0

如果我想發送多個值,該怎麼辦? –

回答

1

要觸發場遠程驗證,您將需要把兩個屬性

parsley-remote="http://yoururl.com" 

parsley-remote-method="POST" 

,以使POST請求提交的變量。

例如:

<input type="text" name="test" value="1" parsley-remote="http://yoururl.com" parsley-remote-method="POST" /> 

那麼你在後端腳本有驗證 「測試」 變量和返回與200響應:

{ "success": "all good!" } 

{ "error": "your custom message" } 
+0

我可以發送多個值到遠程目的地嗎? –

+0

請回應後端文件的例子。 –

+0

更新 - 我只是試圖使用遠程驗證,錯誤消息不會出現。爲了讓Parsley找出驗證失敗,我必須發出一個400返回碼頭。最重要的是,返回一個帶有error屬性的JSON對象(即使具有正確的內容類型頭文件)也不會覆蓋默認的錯誤消息。我最終添加了'data-parsley-remote-message =「我的字段遠程驗證錯誤消息。」。不知道這個答案是用於Parsley的舊版本還是API的變化,或者我只是做錯了。 –

0

在較新版本的Parsley.js中,您可以使用如下所示的data-parsley-remote-options

data-parsley-remote-options='{ 
    "type": "POST", 
    "dataType": "jsonp", 
    "data": { 
     "token": "value" 
    } 
}' 

把你需要發送的任何東西作爲鍵/值對發送到data對象中。

更多信息在the docs