2014-11-21 197 views
0

我是JSON和jqueries的新手。 我CSHTML文件中創建一個驗證碼如下刷新captcha重新加載整個頁面

<div class="row-fluid"> 
<div class="span3"></div> 
<iframe id="CaptchaIfram" src="@Url.Action("ShowCaptchaImage")" scrolling="no"></iframe> 
<div> 
    <div class="span3"></div> 
    <input id="RefreshCaptcha" type="submit" onclick="captcha()" value="Refresh" class="btn btn-success" /> 
</div> 
</div> 
<script type="text/javascript"> 
function captcha() 
{ 
    document.getElementById("CaptchaIfram").contentDocument.location.reload(true); 
} 
</script> 

in Controller: 
public Captcha ShowCaptchaImage(int width, int height, int totalcharacters) 
{ 
    return new Captcha(width, height, totalcharacters); 
} 

其工作正常,如果我點擊刷新按鈕,因爲我使用Url.Action方法整頁刷新得到。 爲了避免這種情況,我使用JSON如下。但圖像沒有得到顯示。 任何人都可以讓我知道我需要糾正的地方。

<div class="row-fluid"> 
<div class="span3"></div> 
<iframe id="CaptchaIfram" onload="showCaptcha()" scrolling="no"></iframe> 
<div> 
    <div class="span3"></div> 
    <input id="RefreshCaptcha" type="submit" value="Refresh" onclick="showCaptcha()" class="btn btn-success" /> 
</div> 
</div>    

<script type="text/javascript"> 
function showCaptcha() 
{ 
    var url = "/ESignature/ShowCaptchaImage"; 
    var target = '#CaptchaIfram'; 
    $.getJSON(url, { width: 200, height: 35, totalcharacters: 5 }, function (data) { 
    document.getElementById("CaptchaIfram").src = data; 
    }); 
} 
</script> 
public JsonResult ShowCaptchaImage(int width, int height, int totalcharacters) 
{ 
    return Json(new Captcha(width, height, totalcharacters), JsonRequestBehavior.AllowGet); 
} 

回答

0

我改變了刷新按鈕的類型爲客戶端按鈕如下。它沒有刷新整個頁面。

不需要Json函數。 (在我的問題中的第二個代碼塊)

相關問題