鍍金池/ 問答/HTML/ 后臺傳給的驗證碼怎么展示在頁面中,因為還要取返回的captchakey?

后臺傳給的驗證碼怎么展示在頁面中,因為還要取返回的captchakey?

我試了把接口地址放在img的src里面,驗證碼可以顯示,但是captchakey怎么???
如果用ajax請求,那么可以取到captchakey,但是和圖片不是對應的,該怎么辦?

這是我寫的demo

<div style="text-align: center" class="container">
    <h1>hahah </h1>
    <img class="img" src="htpp://.....t/displayCaptcha.do" alt="">
</div>

   $('.container').click(function () {
    var a =Math.random() ;
    $.ajax({
        url: 'htpp://.....t/displayCaptcha.do?'+a,
        type: 'get',
        success: function (data, status, xhr) {
            console.log(xhr.getAllResponseHeaders(),'驗證碼') ;**//這里取得的captchakey和下一行賦值的不是一一對應的**
            $(_this).find('.img').attr('src', 'htpp://.....t/displayCaptcha.do?' + a);**//這****里有請求了一次,所以可能key又變了,所以不會一一對應。**
        },
    })
})

這是我請求的demo

回答
編輯回答
舊城人

你這個captchakey應該是后端維護吧,跟你怎么請求沒啥關(guān)系呀

2017年7月22日 16:06
編輯回答
愛礙唉
var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","https://pet.sonystyle.com.cn:9002/eSolverOmniChannel/account/displayCaptcha.do",true);
    xmlhttp.responseType = "blob";
    xmlhttp.onload = function(){
        if (this.status == 200) {
            var blob = this.response;
            var img = document.getElementById("img");
            window.URL.revokeObjectURL(img.src); 
            img.src = window.URL.createObjectURL(blob); 
            console.log(xmlhttp.getResponseHeader("captchaKey"));
        }
    }
    xmlhttp.send();
2017年11月12日 01:55