[英]Need jQuery Dialog for confirmation and Send Ajax Request to Controller Action
[英]AJAX to Controller Password Confirmation
我對AJAX和MVC都比較陌生,我正在嘗試進行確認互動。
我想做的是顯示一個框,用戶將輸入一個值,然后單擊Submit,然后該框將AJAX POST(我認為這是我應該使用的)發送到Controller並返回true或false。 我似乎無法弄清楚如何使這種交互有效。
如果我運行調試,我可以看到我的值被傳遞到Controller中,但是它將忽略我的if
語句來檢查字符串是否正確。 除此之外,我不確定如何在我的AJAX中返回true / false並進行處理。
到目前為止,這是我的“最佳猜測”。
AJAX / JS
<script type="text/javascript">
function preloadFunc() {
var prompting = prompt("Please enter your password", "****");
if (prompting != null && prompting != null) {
$.ajax({
url: '/Admin/magicCheck',
type: 'POST',
data: {
'magic': prompting
},
success: function () {
//let page load
},
error: function () {
preloadFunc(); //re-ask
}
});
}
else {
window.location.replace("google.com"); //Pressing Cancel
}
}
window.onpaint = preloadFunc();
</script>
調節器
[HttpPost]
public ActionResult magicCheck(string magic)
{
bool success = false;
if (magic == "poof")
{
success = true;
}
else
{
success = false;
}
return RedirectToAction("Index");
}
感謝所有的幫助,因為我是新來的!
閱讀本文下方的所有評論,因為這里有很多問題,但要盡量減少AJAX和返回:
更改控制器以返回JSON,如下所示:
[HttpPost]
public ActionResult magicCheck(string magic)
{
bool success = false;
if (magic == "poof")
{
success = true;
}
else
{
success = false;
}
return Json( new { Success = success });
}
現在控制器返回了JSON,您需要更改js來處理它:
$.ajax({
url: '/Admin/magicCheck',
type: 'POST',
data: 'magic=' + prompting, //<-- you didn't need to send JSON for this, you'd have to change your controller if you are going to send a JSON object also... you just need this for now
success: function (resp) {
//let page load
if ( resp.Success ){
// success...
}
else{
//fail
}
},
error: function () {
preloadFunc(); //re-ask
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.