Get an image through jQuery AJAX

六眼飞鱼酱① 提交于 2019-11-28 07:23:14

问题


The following image tag is in my Login Page

<img id="CaptchaImg" alt="Captcha" src="@url.action("Captcha","Login")" style="" />

Now when I refresh my login Page, it works fine, and gets the image from the controller, but when I logOut and the login Page renders .the controller method is not being called.Problem is only in IE works fine on Chrome.Is there are work around? Can I do something with jQuery Ajax call to the controller, I tried this but the success method is not called. Is there any other way?

$.ajax({
    type: "GET",
    url: '/Login/CaptchaImage',
    datatype: "image",
    success: function(data) {
        debugger
        $('#CaptchaImg').attr('src', data);
    }
});

回答1:


Try this

$.ajax({
    type: "GET",
    url: '@Url.action("Captcha","Login")',
    dataType:"image/jpg",
    success: function (data) {
        $('#CaptchaImg').attr('src', data);
      }
 });



回答2:


web Server should return base64 Data. like this

{ base64Data:"blahblahblah" }

your ajax request content-type could be plain json ; in your success function in ajax you could do like this

    success:function(data){
     var src="data:image/png;base64,"+data.base64Data;
      $('#CaptchaImg').attr(src,src)
}


来源:https://stackoverflow.com/questions/17339876/get-an-image-through-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!